Switch statement and while loop
-
I'm trying to write a program that reads state abbreviations from an input file and then outputs the corrosponding state to an output file. (this is my first class in programming) I'm using a while loop that runs until the end of input file and switch statements to match states and abbreviations, here is a portion...
inAbrev >> firstLetter >> secondLetter; //splits abbreviation into two char's while(inAbrev) //read til the end of the file, inAbrev is the ifstream { switch (firstLetter) //checks first letter of abbreviation and tries to match it to working cases //I have one of these for each letter that a state name begins with { case 'A' : switch (secondLetter) { case 'L' : outState << inAbrev << " is " << "Alabama" << endl; //outState is the break; //output file case 'K' : outState << inAbrev << " is " << "Alaska" << endl; break; case 'Z' : outState << inAbrev << " is " << "Arizona" << endl; break; case 'R' : outState << inAbrev << " is " << "Arkansas" << endl; break; }
I can get it to compile but my loop (and maybe my switch statements) seems to be wrong. The problem is that it gets stuck on the first input (AL) and goes into an infinite loop of just reading that input, it is never evaluated and nothing is output. I'm probably missing something simple but my beginners brain can't find it. Any help would be great. Thanks, Aaron arrrgh -
I'm trying to write a program that reads state abbreviations from an input file and then outputs the corrosponding state to an output file. (this is my first class in programming) I'm using a while loop that runs until the end of input file and switch statements to match states and abbreviations, here is a portion...
inAbrev >> firstLetter >> secondLetter; //splits abbreviation into two char's while(inAbrev) //read til the end of the file, inAbrev is the ifstream { switch (firstLetter) //checks first letter of abbreviation and tries to match it to working cases //I have one of these for each letter that a state name begins with { case 'A' : switch (secondLetter) { case 'L' : outState << inAbrev << " is " << "Alabama" << endl; //outState is the break; //output file case 'K' : outState << inAbrev << " is " << "Alaska" << endl; break; case 'Z' : outState << inAbrev << " is " << "Arizona" << endl; break; case 'R' : outState << inAbrev << " is " << "Arkansas" << endl; break; }
I can get it to compile but my loop (and maybe my switch statements) seems to be wrong. The problem is that it gets stuck on the first input (AL) and goes into an infinite loop of just reading that input, it is never evaluated and nothing is output. I'm probably missing something simple but my beginners brain can't find it. Any help would be great. Thanks, Aaron arrrghThe first line should be inside the
while
loop. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo -
The first line should be inside the
while
loop. Joaquín M López Muñoz Telefónica, Investigación y DesarrolloThanks Joaquin, my while loop now works. However for some reason my switch statements are not being run. If you or anyone has any suggestions it would be great. Thanks again, Aaron arrrgh