cin.clear(); only clears the error flags; it doesn't read/discard any characters in the buffer.
For that, you can do something like
1 2 3 4 5 6
|
while (!(cin >> bet))
{
cin.clear(); // Clear error flags
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Also #include <limits> for this
// Print error message here...
}
|
What
cin.ignore(numeric_limits<streamsize>::max(), '\n');
does is basically ignore (discard) as many characters from the input buffer as possible until it reaches a newline (
'\n'
) character.
http://www.cplusplus.com/reference/istream/istream/ignore/
EDIT: Also, in your code, since you don't try to grab a new input if the user enters an invalid one until the next time the
do
-
while
loop loops around, if it so happens that
inputGood is false and whatever junk value is in
bet is negative or greater than
total, then you're going to run right into those other
if
statements as well, which is probably the problem you're trying to describe.
Try replacing the
if
on lines 22 and 32 with
else if
and see if that's any better.