#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()

{

    int num;
    int guess;
    int noOfGuesses;
    bool done;


    cout << "Ready to Play the Guessing Game?" << endl;
    cout << "Think you can beat me? You have 5 Guesses!" << endl;

    num = (rand() + time(0)) % 100;

    done = false;

    while ((noOfGuesses < 5) && (!done))
    {
        cout << "Enter an integer that is between 0 and 100";
        cin >> guess;
        cout << endl;
        noOfGuesses++;

        if (guess == num)
        {
            cout << "Winner! You guessed the correct number!" << endl;
            done = true;
        }
        else if (guess < num)
            cout << "Your guess is lower then the number.\n" "Guess again!" << endl;
        else
            cout << " Your guess is higher then the number.\n" "Guess again!" << endl;
    }
    if (!done)
        cout << "^_^ I win, the number is " << num << endl;

    return 0;
}

Guessing Game program needs to limit the user to only 5 guesses, recieves runtime error can anyone exsplain to me why ?

Recommended Answers

All 4 Replies

When do you get the error? Also, could you please use the CODE tags from now on?

Run - Time Check Failure #3 - The variable 'noOfGuesses' is being used without being initialized.

is the error i get when i run it.

noOfGuesses isn't being set to anything before you're using it in the while loop so it doesn't know if it's less than 5 and it also can't add 1 to it

commented: Helpful +1

lol ok i fixed it now onto my other post to figure that one out lol

thank you for your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.