//////////////////////////////////////////////////////////////////////
//GOOD CHOICE
//////////////////////////////////////////////////////////////////////
int World::GoodChoice(int lowest, int Num, int highest)
{
if ((Num <= lowest) || (Num > highest))
{
//A bad choice so print a message and return false
cout << endl << "I don't understand that choice." << endl;
return 0;
}
//Since we haven't returned, the choice is good
return 1;
}
But you are asking for an integer. 'a' is not an integer, so answer gets the value 0, which is a bad choice according to your code. If you want a character, why aren't you asking for a character?
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there. -- Gordon Bell
thats why I have the error checking in place, if someone types something other than what I want I want it to display the message "I don't understand that choice." then go back to
Item Level: and wait for another entry.
When you enter an "a", the read fails, which sets the
failbit for the stream (cin in your case) AND does not
move the position in the stream (still points to the "a").
If an error occurs, you need to clear the state of the stream
and throw out the rest of the stream buffer :
Code:
cin >> answer;
if (cin.fail())
{
cin.clear();
cin.ignore(100,'\n');
}
Bookmarks