Hi all, I'm still working on my switch statement program (from previous post)thanks to Joaquin my loop is working but for some reason my switch statements are not being activiated. I've included a portion of my code, the switch statements continue for each letter a state begins with (d,f,g,h,i,k,etc) Thanks, Aaron //this program is supposed to read state abbreviations from a file and output the corrosponding state to another file #include #include using namespace std; int main() { ifstream inAbrev("state abs.txt"); //opens the input file ofstream outState("states with abvs.txt"); //opens the output file char firstLetter, secondLetter; inAbrev >> firstLetter >> secondLetter; cout << firstLetter << secondLetter << endl; //just for me to see if anything is happening while(inAbrev) //read til the end of the file { inAbrev >> firstLetter >> secondLetter; cout << firstLetter << secondLetter << endl; switch (firstLetter) { case 'A' : switch (secondLetter) { case 'L' : outState << inAbrev << " is " << "Alabama" << endl; cout << inAbrev << " is " << "Alabama" << endl; //here i'm trying to see if anything is happening break; case 'K' : outState << inAbrev << " is " << "Alaska" << endl; break; case 'Z' : outState << inAbrev << " is " << "Arizona" << endl; break; case 'R' : outState << inAbrev << " is " << "Arkansas" << endl; break; } case 'C' : switch (secondLetter) { case 'A' : outState << inAbrev << " is " << "California" << endl; break; case 'O' : outState << inAbrev << " is " << "Colorado" << endl; break; case 'T' : outState << inAbrev << " is " << "Connecticut" << endl; break; }
arrrgh
Aaron Knox
Posts
-
switch statement -
Switch statement and while loopThanks 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
-
Switch statement and while loopI'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 -
decimal fraction conversionHello All, I need to be able to convert decimal numbers into fraction form. I understand how to do this mathematically but I'm not sure how to put it into code. Thanks for your help Aaron :beer