Do while loop not executing properly
Nov 16, 2010 at 3:03am UTC
My do-while does not execute properly and I'm not sure why. I believe it has something to do with my if...else if statement because the loop executes properly if I isolate either of the if statements. Any help would be greatly appreciated.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
int option;
bool valid;
cout << "Enter an option ((0-6) - 0 to exit) " ;
do
{
cin >> option;
valid = true ;
if (!(cin >> option))
{
cout << "\nInvalid entry. Enter an integer (0-6) " ;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n' );
valid = false ;
}
else if ((option < 0) || (option > 6))
{
cout << option << " is an invalid entry. "
"Enter an integer (0-6) " ;
valid = false ;
}
}while (!valid);
Nov 16, 2010 at 3:30am UTC
You're reading the value twice.
Nov 16, 2010 at 3:43am UTC
Reading the value twice? What do you mean?
Nov 16, 2010 at 8:35am UTC
You read into 'option' on lines 8 and 10. You probably want to change the condition in the if statement on line 10.
Topic archived. No new replies allowed.