Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

C++ guess the number game


Drummerpete
 Share

Recommended Posts

I wrote a little C++ program.
It generates a random number between 1 and 10 and the user has 3 attempts to guess it.

OLD CODE:
```
// Guess the number game
// 11/01/11
// Pete Murray

#include #include #include using namespace std;

int main()
{
    int randomnumber,guess,x; //generated number, user's guess, loop counter
    int endgame; //so user can end game

    //generates random number
    srand(time(0));
    randomnumber = rand()%10+1;

    //prompts user to guess
    cout << "I am thinking of a number between 1 and 10, can you guess what it is? \n" << "You have 3 guesses. \n\n";

    //loop for 3 guesses
    for (x=1; x<4; x++)
    {
        cout << "Guess " << x << ":\n";
        cin >> guess;

        //if guess is correct
        if (guess == randomnumber)
        {
            cout << "Correct, the number was " << randomnumber << "! \n\n";
            cout << "Enter any number to end the game \n";
            cin >> endgame;
            return 0;
        }
        //if guess is wrong
        else
        {
            //guess was too high
            if (guess > randomnumber)
            {
                cout << guess << " is too high. \n\n";
            }
            //guess was too low
            else
            {
                cout << guess << " is too low. \n\n";
            }
        }
    }

    cout << "Enter any number to end the game \n";
    cin >> endgame;
    return 0;
}

```
NEW CODE:
```
// Guess the number game II
// 11/01/11
// Pete Murray

#include #include #include using namespace std;

int main()
{
    int randomnumber,guess,x; //generated number, user's guess, loop counter
    int endgame; //so user can end game

    //generates random number
    srand(time(0));
    randomnumber = rand()%10+1;

    //prompts user to guess
    cout << "I am thinking of a number between 1 and 10, can you guess what it is? \n" << "You have 3 guesses. \n\n";

    //loop for 3 guesses
    for(x=1; x<4 && guess != randomnumber; x++)
    {
        //makes sure the user inputs a number between 1 and 10
        do
        {
            cout << "Please enter a number between 1 and 10: ";
            cin >> guess;
            cout << "\n";
        } while(!guess || guess > 10);

        if(guess > randomnumber) cout << guess << " is too high.\n\n";
        else if(guess < randomnumber) cout << guess << " is too low.\n\n";
        else cout << "Correct!\n\n";
    }

    cout << "The number was " << randomnumber << "!\n\n";
    //prompts user to end the game
    cout << "Enter any number to end the game: ";
    cin >> endgame;

    return 0;
}

```
NEWEST CODE:
```
// Guess the number game II
// 11/01/11
// Pete Murray

#include #include #include using namespace std;

int main()
{
    int randomnumber,guess,x,i; //generated number, user's guess, loop counters
    char endchar; //so user can end game

    //infinite loop
    for(;;)
    {
        //generates random number
        srand(time(0));
        randomnumber = rand()%10+1;

        //prompts user to guess
        cout << "I am thinking of a number between 1 and 10, can you guess what it is? \n" << "You have 3 guesses. \n\n";

        //loop for 3 guesses
        for(x=1; x<4 && guess != randomnumber; x++)
        {
            //makes sure the user inputs a number between 1 and 10
            do
            {
                cout << "Please enter a number between 1 and 10: ";
                cin >> guess;
            } while(!guess || guess > 10);

            if(guess > randomnumber) cout << guess << " is too high.\n\n";
            else if(guess < randomnumber) cout << guess << " is too low.\n\n";
            else cout << "Correct!\n\n";
        }

        cout << "The number was " << randomnumber << "!\n\n";

        //prompts the user to restart game
        cout << "Would you like to play again? (y/n): ";
        cin >> endchar;

        //"clears" screen
        for(i=1; i<100; i++)
        {
            cout << "\n";
        }

        //if user chose to, exit program
        if(endchar == 'n') break;
    }

    return 0;
}

```
Edit:
Now you can play as many games as you want!
I'd still love a programmer's comments.

Game is attached.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...