How to make cin accept a single digit only c++

Viewed 902

So we were challenged by a teacher to make this simple game into a c++ program.

English is not my primary language but I'll try to explain. I'm a beginner so these will not be efficient, but I'm still proud of what I did so far.

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    int secret[3], i = 0, x, y, z, a, b, c, guess = 1, tries = 9;
    bool correct = false;

    srand(time(0));

    do
    {
        secret[i] = rand() % 10;
        i++;
    } while (i <= 2);

    x = secret[0];
    y = secret[1];
    z = secret[2];

    //cout << x << y << z << endl; <--- for debugging purposes

    cout << "=================================================\n\n";
    cout << "       I HAVE THREE SINGLE DIGIT NUMBERS\n\n";
    cout << "  YOU HAVE TEN TRIES TO GUESS ALL THREE DIGITS\n\n";
    cout << "=================================================\n\n";

    do
    {
        cout << "\n\nGuess the first digit.\n";
        while (!(cin >> a))
        {
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cout << "Invalid input. Please try again: \n";
        }

        cout << "\n\nGuess the second digit.\n";
        cout << a;
        while (!(cin >> b))
        {
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cout << "Invalid input. Please try again: \n" << a;
        }

        cout << "\n\nGuess the third digit.\n";
        cout << a << b;
        while (!(cin >> c))
        {
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cout << "Invalid input. Please try again: \n" << a << b;
        }

        cout << "\n\n====================================================\n\n";

        if (tries == 0)
        {
            cout << "YOU RAN OUT OF GUESSES! \n";
            cout << "The secret number is " << x << y << z << "!\n";
            cout << "PLEASE RESTART THE PROGRAM TO TRY AGAIN!\n";
            correct = true;
        }
        else if ((a == x) && (b == y) && (c == z))
        {
            cout << "YOU GUESSED THE SECRET NUMBER IN " << guess << " TRY / TRIES!\n";
            cout << "CONGRATULATIONS!\n\n";
            correct = true;
        }
        else if ((a == x) && (b == y) && (c != z))
        {
            cout << "You guessed TWO of the numbers correctly!\n";
            cout << "You have " << tries << " tries left.\n";
            correct = false;
        }
        else if ((a == x) && (b != y) && (c != z))
        {
            cout << "You guessed ONE of the numbers correctly!\n";
            cout << "You have " << tries << " tries left.\n";
            correct = false;
        }
        else if ((a == x) && (b != y) && (c == z))
        {
            cout << "You guessed TWO of the numbers correctly!\n";
            cout << "You have " << tries << " tries left.\n";
            correct = false;
        }
        else if ((a != x) && (b == y) && (c == z))
        {
            cout << "You guessed TWO of the numbers correctly!\n";
            cout << "You have " << tries << " tries left.\n";
            correct = false;
        }
        else if ((a != x) && (b == y) && (c != z))
        {
            cout << "You guessed ONE of the numbers correctly!\n";
            cout << "You have " << tries << " tries left.\n";
            correct = false;
        }
        else if ((a != x) && (b != y) && (c == z))
        {
            cout << "You guessed ONE of the numbers correctly!\n";
            cout << "You have " << tries << " tries left.\n";
            correct = false;
        }
        else
        {
            cout << "You guessed NONE of the numbers correctly!\n";
            cout << "You have " << tries << " tries left.\n";
            correct = false;
        }

        cout << "\n====================================================\n\n";
        guess++;
        tries--;
    } while (correct == false);


}

I have a little problem though, in this part of the program,

cout << "\n\nGuess the first digit.\n";
        while (!(cin >> a))
        {
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cout << "Invalid input. Please try again: \n";
        }

I could enter anything invalid and it will properly identify it as an invalid input. But if I enter 2 digits (22 for example), the program still enters 22 and it just accepts it.

I don't know exactly how that part of my program works, I just copy-pasted it. Is it possible to modify it, or my program to only accept a single digit, 0-9, and identify the input as invalid when two numbers are entered?

I know it's just a minor inconvenience and doesn't really break the program, but if I can make it better then it would be great. I just want it to be perfect, if possible.

I'm guessing if there's something like _getche for integers then it would be better?

Thanks in advance.

1 Answers

while (!(cin >> a)) means keep looping while not able to convert the input to an integer value in a - you can add extra conditions:

while (!(cin >> a) || a < 0 || a > 9)

In those latter cases, there's no need to .clear() and .ignore(...) but it won't do any harm.

In case it's new to you, || means logical-or, the equivalent logic to English such as: "a is less than 0 OR a is greater than 9".

Related