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

        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;
            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 ?

Edited 3 Years Ago by diafol: fixed formatting

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

Edited 3 Years Ago by happygeek: fixed formatting

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


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

thank you for your help

This article has been dead for over six months. Start a new discussion instead.