Problem with my loop and switch statement.

So I wrote this code and everything works fine but this one spot. When a user types in an in valid key and this only happens if it goes to the default statement the program will go into an infinite loop. Otherwise every other scenario works beautiful. I put a return 0; at the end of the default statement to stop this infinite loop and that worked but now it won't ask the user if they wish to continue. As you can guess this is for a class but the program did not require us to have loops I just added them because I thought it would make life easier.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133`` ``````#include #include #include using namespace std; int main() { // Decalare variables. int number; float amountDue, hours; string input; // Asks user the package subscription they have. cout <<"Select a subscription package:\n"; cout <<"1.Package A \n"; cout <<"2.Package B \n"; cout <<"3.Package C \n"; cout <<"4.Quit \n"; cin >> number; // Decalare value for loop bool cont = true; // Using while loop for the program to loop. while (cont) { // switch statement switch (number) { // Package A case 1: cout << "How many hours was used? "; cin >> hours; // if customer used less than 10 hours it will be a flat 9.95 charge. if(hours >= 0.00 && hours <= 10.00) { amountDue = 9.95; cout << "The total amount due is \$" << amountDue << endl; } // if custiomr used more than 10 hours it will be a charge of 9.95 plus 2 for each addtional hour. else if(hours > 10.00 && hours <= 744.00) { amountDue = 9.95 + 2.00*( hours - 10); cout << "The total amount due is \$" << amountDue << endl; } //if user typed in an invalid number. else if(hours > 744.00 || hours < 0) { cout << "The hours used must be between 0.00 and 744.00." << endl; } break; //Package B case 2 : cout << "How many hours was used? "; cin >> hours; if(hours >= 0.00 && hours <= 20.00) { // if the customer used less than 20 hours a flat 14.95 charge. amountDue = 14.95; cout << "The total amount due is \$" << amountDue << endl; } // if the sutomer used more than 20 hours a charge of 14.95 plus 1 for each addtional hour. else if(hours > 20.00 && hours <= 744.00) { amountDue = 14.95 + 1.00*( hours - 20); cout << "The total amount due is \$" << amountDue << endl; } //if user typed in an invalid number. else if(hours <0.00 || hours > 744.00) { cout << "The hours used must be between 0.00 and 744.00." << endl; } break; //Package C case 3 : cout << "How many hours was used? "; cin >> hours; // if user types any valid number a flat charge of 19.95. if(hours >= 0.00 && hours <= 744.00) { amountDue = 19.95; cout << "The total amount due is \$" << amountDue << endl; } //if user typed in an invalid number. else if(hours <0.00 || hours > 744.00) { cout << "The hours used must be between 0.00 and 744.00." << endl; } break; // quiting program case 4 :cout << "Press enter to quit program."; break; // catch all other input /* Note: I could not get my loop to work with default and it is only in this case. I tried removing the return 0; but it would just go into an infinite loop I did several hours of resreach and could not find out why.maybe give me a hint? or possible have an answer as to why*/ default: cout <<"The valid choices are 1 through 4.\n"; cout <<"Run the program again and select one of those."; return 0; } //Asks users if they wish to continue using the program. cout << "Do you wish to continue? (Press Y/N)" << endl; cin >> input; if(input == "Y" || input == "y") { // Asks user the package subscription they have. cout <<"Select a subscription package:\n"; cout <<"1.Package A \n"; cout <<"2.Package B \n"; cout <<"3.Package C \n"; cout <<"4.Quit \n"; cin >> number; } else {cont=false;} { } } cout <<"\n Press Enter to quit."<
Last edited on
If a read operation fails cin will be enter an error state where all future read operations will fail. You can check if an operation failed by putting it inside an if statement. To leave the error state you can use the clear function. You will also have to remove the invalid input from cin because it's still there. You can do that by using ignore.
 ``12345`` ``````if (cin >> number) { cin.clear(); cin.ignore(numeric_limits::max(), '\n'); }``````
Last edited on
Okay I am confused on where should I put this? should it be inside of my default? and is there a header I need to use for it?

If a read operation fails cin will be enter an error state where all future read operations will fail. You can check if an operation failed by putting it inside an if statement. To leave the error state you can use the clear function. You will also have to remove the invalid input from cin because it's still there. You can do that by using ignore.
 ``12345`` ``````if (cin >> number) { cin.clear(); cin.ignore(numeric_limits::max(), '\n'); }``````

Note that it should be `if (!(cin >> number))`

 Okay I am confused on where should I put this?

You should do it whenever you ask someone to enter a number:

 ``1234`` ``````double db; cout << "Please enter a number: "; cin >> db; cout << "You entered: " << db << ". Thanks for playing\n";``````
 ``12345678910`` ``````int db; cout << "Please enter a number: "; if (!(cin >> db)) { cin.clear(); cin.ignore(80, '\n'); cout << "Hey, I asked for a number\n"; } else cout << "You entered: " << db << ". Thanks for playing\n";``````