1

我正在学习 C++,但我有一个我无法弄清楚的问题。当我输入 X 个字符时,当前的字符数将导致“失败”消息显示 X 次。

截屏,

在此处输入图像描述

这是我的代码,

#include <iostream>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <ctype.h>

using namespace std;

int generateNumber()
{

    int randomNumber;

    srand(time(0));
    randomNumber = rand() % 100 + 1;

    return randomNumber;

}

int main()
{

    // Predefine vars
    int lifes = 5;
    int attempts = 0;
    int randomNumber = generateNumber();
    int gameStarted = 0;
    int gussedNumber;
    int score = 0;
    string username;

    cout << "--------------------------------" << endl;
    cout << "------- GUESS THE NUMBER -------" << endl;
    cout << "---------- VERSION 1.0 ---------" << endl;
    cout << "--------------------------------" << endl << endl;

    while(gameStarted == 0)
    {
        cout << "Enter your username before we get started" << endl;
        cin >> username;
        gameStarted = 1;
        system("cls");
    }

    if(gameStarted == 1)
    {

        cout << "Welcome, " << username << "!" << endl;
        cout << "Guess a number between 1 - 100" << endl << endl;

        while(attempts <= lifes)
        {

            // If you dont have any more lifes left
            if(lifes == 0)
            {

                system("CLS");
                cout << "Game Over" << endl;
                cout << "Lets try again." << endl;
                cout << "Guess a number between 1 - 100" << endl << endl;
                lifes = 5;
                randomNumber = generateNumber();

            } else {

                cin >> gussedNumber;

                if(cin)
                {

                    if(gussedNumber == randomNumber)
                    {

                        // Correct Number
                        system("CLS");
                        cout << "ConGratz Bro, you hit the correct number!" << endl;
                        cout << "Lets try again. You now have 5 lifes left." << endl;
                        lifes = 5;
                        score = score + 1;

                        // Generate a new random number
                        randomNumber = generateNumber();

                    } else {

                        // Wrong Number
                        lifes = lifes - 1;
                        cout << "Wrong Number." << endl;
                        cout << "Number of lifes left: " << lifes << endl << endl;

                    }
                } else {
                    cin.clear();
                    cin.ignore();
                    cout << "That was not a number!" << endl;
                }
            }
        }
    }

    cout << randomNumber << endl;
    return 0;
}

只是我在学习时正在做的一个简单程序。

4

2 回答 2

1
cin.ignore();

你只忽略一个字符。您需要忽略整行:

cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

(您需要包括limitsfor std::numeric_limits)。另请参阅std::basic_istream::ignore,这正是解决您的问题。

于 2013-03-15T11:15:07.840 回答
0

我删除了所有不相关的代码并得到了这个:

#include <iostream>

int main()
{
    for(int attempts=0; attempts < 10; ++attempts) {
        int guessedNumber;
        cin >> guessedNumber;
        if(cin) {
        } else {
            cin.clear();
            cin.ignore();
            cout << "That was not a number!" << endl;
        }
    }
    return 0;
}
于 2013-03-15T13:10:38.393 回答