如何检查输入是否为数字,以及输入是否介于0和100之间?

时间:2019-04-03 17:14:23

标签: c++

我需要测试输入是否为数字,如果为0到100之间,则为数字。如果不是,则应输入LOL NOOB。

我尝试制作一个具有列表的变量,它会查看数字是否在列表中,如果数字不适合列表,它会显示NAN

while (title == 1) {
    ++level;

    system("cls");
    //variables
    char p;
    int secret, guess;
    float NumberDetection;
    NumberDetection = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100;

    // color
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);

    //the number that you guess!
    srand(time(NULL));
    secret = rand() % 100 - 0;


    cout << "  The Legit Guessing Game!" << endl << "   The Dankest of its kind!" << endl;
    cout << "-------------------------------------" << endl;
    cout << endl;
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 6);
    cout << "Welcome my name is Luffy Computron. your currant level is " << level << endl;
    cout << " I will randomly pick a number between 0 and 100" << endl;
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 22);
    cout << "Take a guess" << endl;
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
    cout << "Guess:";
    cin >> guess;
    if (guess == NumberDetection) {
        while (guess != secret) {
            if (guess > secret) {
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 6);
                cout << "Too large. Try again." << endl;
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
            }
            if (guess < secret) {
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 6);
                cout << "Too small. Try again." << endl;
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
            }
            cout << "Guess:";
            cin >> guess;
        }

        if (guess == secret) {
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 6);
            cout << "Congradulations! The number was " << secret << endl;
            if (level == 1) {
                cout << "you are now an untrained aprentice of the computron team, ";
                cout << "to become an aprentice play 3 more times!";

            }
            else if (level == 2) {
                cout << "you are now an untrained aprentice of the computron team ";
                cout << "to become an aprentice play 2 more times!";

            }
            else if (level == 3) {
                cout << "you are now an untrained aprentice of the computron team ";
                cout << "to become an aprentice play 1 more times!";

            }
            else if (level == 4) {
                cout << "Congradulations you Leveled up to an aprentice of the computron team! ";
                cout << "Only 5 games left to level up to Journeyman!";

            }
            else if (level == 9) {
                cout << "Wow you are level 9!! that means you are a Journeyman! Congradulations!" << endl << "You have reached the top rank for now... But there is more to come!";

            }
            else
                cout << "you have passed my expectations! Con-drag-ulations!";
            cout << " Do you want to play again and better your guessing skills? " << endl;
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
            cout << "[Y] Yes";
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
            cout << "[N] No" << endl;
            cin >> p;

            if (p == 'N' || p == 'n') {
                title = 0;
            }

            else if (p == 'y' || p == 'Y') {
                title = 1;
            }
        }
        Sleep(30);
        while (title == 0) {
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
            cout << "/";
            Sleep(10);
            system("cls");
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 6);
            cout << "|";
            Sleep(10);
            system("cls");
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14);
            cout << "\\";
            Sleep(10);
            system("cls");
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
            cout << "--";
                            Sleep(10);
            system("cls");
            return 0;
        }
    }
    else { 
    cout << "THATS NOT A NUMBER !!!!!!!!!!!!!!!!!!!!!!!!!!!";
    Sleep(10000);

    return 0;


    }
}

预期的结果是它说的不是数字,然后关闭。但相反,它会向“垃圾邮件再试一次”发送垃圾邮件。

1 个答案:

答案 0 :(得分:1)

在获取用户输入时使用条件语句

 int i;
 cout<<"Enter Value";
 if(cin>>i)
 {
     cout<<"value is Integer";
     if(i>=0 && i<=100)
     cout<<"value is between 0 and 100";
 }