Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check for a valid user input in C++

Tags:

c++

I'm trying to build the Bulls & Cows game in C++. I've implemented most of the logic. The game runs continuously with the use of an infinite loop and generates a random value at each run.

What I'm trying to do now is to now is to take the user input and run the code if the input is valid (can ONLY be a 4 digit integer). This is my implementation:

#include ...

using namespace std;

vector<int> getDigits(int modelValue) {
    vector<int> vectorValue;
    int extractedDigit = 0;
    int modulant = 10000;
    int divisor = 1000;

    for (int i = 0; i < 4; i++) {
        extractedDigit = (modelValue % modulant) / divisor;
        vectorValue.push_back(extractedDigit);
        modulant /= 10;
        divisor /= 10;
    }return vectorValue;
}


int main() {
    for (;;) {
        int model = rand() % 9000 + 1000;
        int guess = 0000;
        int bulls = 0;
        int cows = 0;
        int counter = 1;

        cout << "This is the random 4-digit integer: " << model << endl;
        cout << "Enter a value to guess: ";
        cin >> guess;

        if ((guess >= 1000) && (guess <= 9999) && (cin)) {

            vector<int> modelVector = getDigits(model);
            vector<int> guessVector = getDigits(guess);

            for (int i = 0; i < 4; i++) {

                if (find(modelVector.begin(), modelVector.end(), guessVector[i]) != modelVector.end()) {
                    if (modelVector[i] == guessVector[i]) { bulls += 1; }
                    else { cows += 1; }
                }
            }cout << "There are " << bulls << " bulls and " << cows << " cows" << endl;
        }
        else {
            cout << "Please enter a valid 4-digit integer between 0000 and 9999" << endl;
            cin.clear();
        }
    }return 0;
}

But when I run and input something invalid, what I get is a continuously running console.

like image 904
oo92 Avatar asked Nov 08 '22 07:11

oo92


1 Answers

There's nothing wrong with the way you read the user input, it just doesn't check for the input type before assigning the value into your 'guess' variable.

So, if an user put any value that isn't accepted by the integer type it would crash your application generating this infinite loop.

To protect your integer variable from wrong user inputs you must replace your direct input assignment:

cin >> guess;

By a protected one:

while(!(cin >> guess) || (guess < 1000)){
    cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    cout << "Invalid input. Please, try again: ";
}

Into the while above you can see the "numeric_limits::max()" which is explained here:

Returns the maximum finite value representable by the numeric type T. Meaningful for all bounded types.

At the end you have a while holding the user into this reading loop while its input is under 1000 (as requested) or isn't a valid integer.

like image 147
Antonio Alecrim Avatar answered Nov 14 '22 21:11

Antonio Alecrim