I am having issues with the code bellow. A programmer pointed out to me that I am going into an infinite loop if someone enters and invalid character. I have a portion of the code bellow but I have done the same thing in multiple places. The man also said I could remove it and incorporate it into the default of the switch statement....but how do I do this?
int main()
{
int value;
do {
do {
cout <<"Would you like to convert the weight or length:\n""Press 1 for Lengths\n""Press 2 for Weight or\n""Press 0 to terminate program\n";
cin >> value;
if (value != 0 && value != 1 && value != 2)
cout << "Invalid entry." << endl;
} while (value != 0 && value != 1 && value != 2);
switch (value)
{
case 0:
break;
case 1: convert_lengths();
break;
case 2: convert_weights();
break;
}
} while(value != 0);
return 0;
}
if (value != 0 && value != 1 && value != 2)
cout << "Invalid entry." << endl;
} while (value != 0 && value != 1 && value != 2);
Oh I am sorry. You did not properly formatted the code that I interpretated it incorrectly.
I think that your prohtammer meant that instead of a number some letter can be entered. In this case the state of the input stream std::cin will be not active.
How do I do that? I am a beginner at C++ and there for only know bigger language but thank you for your help....just please show me the code you want me to enter!
If you enter anything that can't be resolved to an integer type, you're going to put cin into a fail state and you need to reset its flags. That's what cin.clear() is for.
Instead of this:
1 2
if (value != 0 && value != 1 && value != 2)
cout << "Invalid entry." << endl;
try this:
1 2 3 4 5 6 7 8
if (value != 0 && value != 1 && value != 2)
{
cout << "Invalid entry." << endl;
//clear cin's flags
cin.clear();
//remove everything in the buffer up to and including the first newline
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
You may have to #include <limits> .
Vlad's alternative of setting value = 0; will mean that any time cin can't coerce your input into an integer that value will remain zero, and you will get the behavior associated with the user putting in 0. In your case, the program would exit gracefully if the user inputs garbage.
Instead of all those do loops & code to check for invalid input, have default: clause in the switch, and have a quit case as well (your option 0 does that). The quit case should set a bool variable which ends the enclosing while loop:
bool Quit = false;
while (!Quit) {
//get input
switch (MenuOption) {
case 0:
Quit = true; // execution continues after the while loop
// could use return 0; here or the exit function to end the program
break;
case 1:
convert_lengths();
break;
case 2:
convert_weights();
break;
default:
std::cout << "Bad Input, Enter 0, 1 or 2"break;
} // end of switch
} // end of while
I personally avoid do loops (I can mostly find a way to write them as while or for), and I really hate dislike constructs like these, No I really do hate them 8+) :
if (value != 0 && value != 1 && value != 2)
Also remember that break only breaks out of the inner most loop.
int main()
{
int value;
do {
cout <<"Would you like to convert the weight or length:\n""Press 1 for Lengths\n""Press 2 for Weight or\n""Press 0 to terminate program\n";
cin >> value;
switch (value)
{
case 0:
break;
case 1: convert_lengths();
break;
case 2: convert_weights();
break;
default:
if (value != 0 && value != 1 && value != 2)
cout << "Invalid entry." << endl;
break;
}
} while(value != 0);
return 0;
}