I am trying to code a simple question and number checker into my first C++ program. Problem is, when I type a string like one two, or three, the program becomes and infinite loop and it ignores the cin function to re-assign lives to a number.
cout << "How many lives would you like 1 (hard), 2 (medium), or 3 (easy)?" << endl;
cin >> lives;
while(lives != 1 && lives != 2 && lives != 3 && !isdigit(lives))
{
cout << "You need to input a number, not words." << endl;
cout << "How many lives would you like 1 (hard), 2 (medium), or 3 (easy)?" << endl;
cin >> lives;
}
Here is my current code with your suggestions:
cout << "How many lives would you like 1 (hard), 2 (medium), or 3 (easy)?" << endl;
std::cin.ignore();
std::cin.clear();
if (std::cin >> lives)
{
while(lives != 1 && lives != 2 && lives != 3)
{
cout << "You need to input a number, not words." << endl;
cout << "How many lives would you like 1 (hard), 2 (medium), or 3 (easy)?" << endl;
cin >> lives;
}
}
#include <iostream>
#include <limits>
int main()
{
int lives = 0;
std::cout << "How many lives would you like 1 (hard), 2 (medium), or 3 (easy)?" << std::endl;
while(!(std::cin >> lives) || lives < 1 || lives > 3)
{
std::cout << "You need to input a number, not words." << std::endl;
std::cout << "How many lives would you like 1 (hard), 2 (medium), or 3 (easy)?" << std::endl;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
return 0;
}
Alright. std::cin.clear();
takes care of resetting the fail bits. std::cin.ignore
removes any wrong input left in the stream. And I've adjusted the stop condition. (isDigit
was a redundant check, if lives is between 1 and 3 then clearly it's a digit).
When std::istream
can't read a value, it goes into failure mode, i.e., std::failbit
is set and the stream yields false
when tested. You always want to test if a read operation was successful:
if (std::cin >> value) {
...
}
To restore the stream to good state you'd use std::cin.clear()
and you probably need to ignore bad characters, e.g., using std::cin.ignore()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With