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.
#include <iostream>
#include <cstdlib>
#include <string>
usingnamespace 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.
elseif(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.
elseif(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.
elseif(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.
elseif(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.
elseif(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."<<endl;
return 0;
}
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.
1 2 3 4 5
if (cin >> number)
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
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.
1 2 3 4 5
if (cin >> number)
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
You should do it whenever you ask someone to enter a number:
Instead of:
1 2 3 4
double db;
cout << "Please enter a number: ";
cin >> db;
cout << "You entered: " << db << ". Thanks for playing\n";
You want:
1 2 3 4 5 6 7 8 9 10
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";