如何防止char输入影响int变量

时间:2015-02-28 00:04:41

标签: c++

我有一个循环,正在寻找1到100之间的int。如果用户输入一个字母,循环就会变得疯狂。如何将字符和字符分开?更多细节,更多细节,更多细节,更多细节

#include<iostream>
#include<time.h>
using namespace std;

int main()
{   
const int GUESSES = 5;                // The user gets 5 guesses, that never changes
int randomNumber = -1;                // Initializing the random number
int guessesRemaining = GUESSES;       // Initializing the guesses remaining
int userGuess = -1;                   // Initializing the user guess variable

srand(time(0));                       // srand is using the time function to generate a random number
randomNumber = rand() % 100 + 1;      // rand is using the modulus operator to divide the ms by 100 then adds 1 to the solution

// original instructions for the user
cout << "Welcome to the number guessing game. The computer will select a number between 1";
cout << "and 100. It is your job to guess this secret number. You are allowed 5 guesses.\n\n";

do
{
    //cout << randomNumber;
    // user prompt
    cout << "Please enter a number between 1 and 100: ";

    // user input
    cin >> userGuess;

    // if the users guess is less than 100 and greater than 0
    if (userGuess < 100 && userGuess > 0)
    {
        // if user answers correctly
        if (userGuess == randomNumber)
        {
            switch (guessesRemaining)
            {
            case 5:
                cout << "\nHoly crap! That's amazing! One shot, one kill!\n";
                break;
            case 4:
                cout << "\nWow, you're pretty good! You got it in two guesses!\n";
                break;
            case 3:
                cout << "\nNot bad. You got it in three guesses.\n";
                break;
            case 2:
                cout << "\nMan, that was a close one! You had one guess left.\n";
                break;
            case 1:
                cout << "\nBy the skin of your teeth! You got it on your last guess!!\n";
                break;
            }
            break;
        }
        // if user does NOT answer correctly and still has guesses remaining
        else if (--guessesRemaining > 0 && userGuess != randomNumber)
        {
            // notifies user guess is too high and displays remaining guesses.
            if (userGuess > randomNumber)
            {
                cout << "\nYour guess is too high, please try again. You have " << guessesRemaining << " guesses left.\n";
            }
            // notifies user guess is too low and displays remaining guesses.
            else
            {
                cout << "\nYour guess is too low, please try again. You have " << guessesRemaining << " guesses left.\n";
            }
        }
    }
    // notifies user of invalid input.
    else
    {
        cout << "\nDo you know how to read? Try again.\n";
    }
} while (guessesRemaining > 0); // end of do/while loop, as long as remaing guesses are more than 0

// if user doesn't guess within the 5 guesses, notifies of loss and what winning number was.
if (guessesRemaining < 1)
{
    cout << "\nWow, you suck. The number is " << randomNumber << endl << endl;
    system("pause");
    return 0;
}

system("pause");
return 0;

}

1 个答案:

答案 0 :(得分:0)

这可以通过在将其用作一个之前简单地检查输入的值实际上是整数来实现。由于您没有提供语言,一般的想法是......

Read input into X
Check that X is in fact an integer (atoi, etc)
Use the value of X as an integer, most likely in another variable