7

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;
    }

}
Logan Saso
  • 73
  • 1
  • 1
  • 5

2 Answers2

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

Borgleader
  • 15,826
  • 5
  • 46
  • 62
3

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

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
  • Diet, it has the same issue is add the two lines before the while loop. What would "value" bee in your example there. Lives...? – Logan Saso Aug 03 '13 at 19:56
  • `value` is whatever you want want to read, e.g., an `int`. BTW, your test for `isdigit()` won'twork if `lives` is an `int`: this function operates on `char`s. I'm pretty sure that the issue is as described and you may want to print what was received... – Dietmar Kühl Aug 03 '13 at 20:01
  • Ok, When using the if statement, it never goes to the while loop at all. Even if I type three or tree, it just says my lives are saved which wont work later with my lives--. – Logan Saso Aug 03 '13 at 20:05
  • Are you trying to enter `three` to be read as an `int`? That will fail! Try using `3`. If you want to support entering `one`, `two`, and `three` you'll need to implement the parsing from strings yourself. – Dietmar Kühl Aug 03 '13 at 20:22
  • No that was not what I was doing. – Logan Saso Aug 03 '13 at 20:35