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).