Hi everyone, I'm new and I have a loop problem.

First off hi everyone. I was referred to this badass site by a fellow CS major.

I am having trouble with my do-while loop and its making my program hang off and continue endlessly. What can I do to make it read from "file1" and write to "file2"? Here is the code:




#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main () {

string inputFileName = "file1";
string outputFileName = "file2";
ifstream messageInFile;
ofstream messageOutFile;

char character;
string decrementedMessage;


messageInFile.open(inputFileName.c_str());

messageInFile >> character; // grabs first character
do {
character = toupper(character); // forces any lowercase letters to uppercase

switch(character) {

case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
character = character--; // if character matches a case, decrement it by one
break;
}

if (character == 'A') {
character = '9';
}

if (character == '0'){
character = 'F';
}

decrementedMessage = decrementedMessage + character; // add each character back into a message

} while (messageInFile);

messageInFile.close();
messageOutFile.open(outputFileName.c_str());

messageOutFile << decrementedMessage;

messageOutFile.close();


return 0;
}

Last edited on
Wow,,,,,,,,I am a total idiot.

I forgot to add another command to grab the next character at the end of my do-while loop.

Well, Im still glad I found this website and since I suck at coding I'll be back soon enough! : )
Topic archived. No new replies allowed.