简体   繁体   中英

C++ message getting printed multiple times

I'm learning C++, but I have an issue that I can't figure out. When I type X amount of characters, the current number of characters will result in the "failed" message being displayed X amount of times.

Screenshot,

在此处输入图片说明

and here is my code,

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

Just a simple program I'm doing while learning.

cin.ignore();

You ignore just one character. You need to ignore the whole line instead:

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

(You need to include limits for std::numeric_limits ). See also std::basic_istream::ignore , which exactly tackles your problem.

I chopped out all the irrelevant code and got this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM