2

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.

  • `a` should be of type `char`, no? You can test if its a digit using the [`std::isdigit()`](https://en.cppreference.com/w/cpp/string/byte/isdigit) function, or write your own. To get the numeric value of the digit character simply use `a - '0'` then. – πάντα ῥεῖ Oct 29 '20 at 18:20
  • why don't you make a check whether it's two-digit number or a single digit number.? – y_159 Oct 29 '20 at 18:21
  • @y_159 Yes of course I've seen that, that's why I asked why it's not a single `char`, if the OP wants single digits inputs. – πάντα ῥεῖ Oct 29 '20 at 18:24
  • *I'm a beginner so these will not be efficient* - One of the things you learn as you get more experienced is that there are more important things than efficiency. In fact it's a bit puzzling why beginners rate efficiency so highly. – john Oct 29 '20 at 20:03
  • I didn't know you could "convert" a char digit to it's numeric value before I read your comment. That would be very handy. Thank you. – Agapito Hampaslupa Oct 30 '20 at 03:17
  • Well, it's just that, as a beginner, I thought that someone with more experience would write the program with less code using an elegant solution. Good to know that efficiency is not the most important. Thank you – Agapito Hampaslupa Oct 30 '20 at 03:22

1 Answers1

3

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

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252