So I figure I'll put this here since I had to traverse a lot of docs and forums to find the definitive answer. I was trying to get input from the user and check if the input was an integer using isdigit() in an if statement. If the if statement failed the program would output an error message. Although, when a nondigit character was entered the program would loop through the error message endlessly. Here's that code:
int guess = -1;
while (game.getCurQuestion() <= 4) {
std::cout << "Guess: " << game.getCurQuestion() + 1 << std::endl;
std::cin >> guess;
if(isdigit(guess))
{
game.guess(guess);
else
{
std::cout << "Error\n"; //this would be looped endlessly
}
}
std::cout << "You got " << game.getCorrect() << " correct" << std::endl;
return 0;
}
NOTE: Solved, only posted to include my solution. Feel free to correct if I stated anything incorrectly.
Problem: The program kept hold of the non-integer value stored in the cin buffer. This leads to the program never leaving the error message.
Solution:
std::cin.fail()to check if the input matches the variable data type. I.E.intwas the expected input but the user entered achar. In this casestd::cin.fail()would be true.std::cin.fail(), usestd::cin.clear()andstd::cin.ignore(std::numeric_limits<int>::max(), 'n')std::cin.clear()will clear the error flag. Thestd::cin.ignore(std::numeric_limits<int>::max(), 'n')will ignore any other input that is not an integer and will skip to the new line. Effectively progressing the program.The solution implemented in my code looks like this:
Hope this helps and that it saves some people the tedious research because of never learning
std::cinerror handling! Note: I'm aware my implementation skips the current move, call it punishment ;)