Problem: letters in in variables

Writing a Roulette simulation program. I am working on making on debugging my program, and trying to make it so it cannot be broken. I know there is some simple solution here, but I can't recall how its done.

Problem: I ask the user to enter a number to be used in a switch statement. When a number is entered, everything is fine. The problem occurs when anything other than a number is entered. I want to supply an error message when an incorrect character (anything other than 1, 2, or 3) is entered by the user.


void betType(int &betTypeV, int &exact)
{
cout << endl;
cout << "To bet on the Odd numbers press 1, to bet on the Even numbers press 2, and enter 3 to bet on an Exact number: ";
cin >> betTypeV;
cout << endl;

while (betTypeV < 1 || betTypeV > 3)
{
cout << "I'm sorry, that is not a valid option." << endl;
cout << "Enter 1 to bet on odd numbers, a 2 for evens, or a 3 to bet on an exact number: ";
cin >> betTypeV;
}


switch (betTypeV)
{
case 1: cout << "You are betting on all odd numbers." << endl;
cout << endl;
break;

case 2: cout << "You are betting on all even numbers." << endl;
cout << endl;
break;

case 3: cout << "You are betting on an exact number." << endl;
cout << endl;
if (betTypeV == EXACT)
{
cout << "You may bet any of the numbers 1-36. What number would you like to bet on? ";
cin >> exact;
cout << endl;
}
}
}
I think cin.good() returns false if the last input was invalid.
Topic archived. No new replies allowed.