如何使cin只接受单个数字c++; 所以我们被老师挑战,把这个简单的游戏变成了一个C++程序。

如何使cin只接受单个数字c++; 所以我们被老师挑战,把这个简单的游戏变成了一个C++程序。,c++,validation,cin,C++,Validation,Cin,英语不是我的主要语言,但我会尽力解释。我是一个初学者,所以这些将不会是有效的,但我仍然为我迄今为止所做的感到自豪 #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

英语不是我的主要语言,但我会尽力解释。我是一个初学者,所以这些将不会是有效的,但我仍然为我迄今为止所做的感到自豪

#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);


}
#包括
#包括
#包括
使用名称空间std;
int main()
{
int secret[3],i=0,x,y,z,a,b,c,guess=1,trys=9;
布尔正确=错误;
srand(时间(0));
做
{
秘密[i]=rand()%10;
i++;
}while(i
while(!(cin>>a))
表示在无法将输入转换为
a
中的整数值时保持循环-您可以添加额外的条件:

while (!(cin >> a) || a < 0 || a > 9)
while(!(cin>>a)| a<0 | a>9)
在后一种情况下,没有必要
.clear()
.ignore(…)
,但这不会造成任何伤害


如果您对它不熟悉,
|
表示逻辑或,与英语相当的逻辑,例如:“a小于0或a大于9”.

a
应为
char
类型,否?您可以使用该函数测试其是否为数字,或编写自己的。要获取数字字符的数值,只需使用
a-'0'
即可。您为什么不检查一下它是两位数还是一位数?@y_159是的,我当然看到了,这就是为什么我是如果OP需要一位数的输入,为什么它不是一个单字符呢?我是一个初学者,所以这些输入不会有效率。随着你经验的增加,你会学到的一件事是,有比效率更重要的事情。事实上,有点令人困惑,为什么初学者的效率如此之高。我不知道你可以“转换”在我阅读你的评论之前,把它的数值改成一个字符数字。那将非常方便。谢谢。这就是我要找的。谢谢!@AgapitoHampaslupa如果这篇文章回答了你的问题,请考虑接受它!
while (!(cin >> a) || a < 0 || a > 9)