jeudi 4 novembre 2021

Random number generator 1 and 10 [closed]

Hi so I need to make a program that asks a number between 1 and 10. I have provided an example out put of what the program is supposed to output. However it seems like it is not asking for a random number but set to the designated input.

This is what I have come up with: My problem is that if I am approaching this problem wrong. I have the random, but seems like the user input is set if it is stopping at the user input.

int main()
{
    int num, guess, tries = 0;
    srand(time(0)); //seed random number generator
    num = rand() % 10 + 1; // random number between 1 and 10

    do
    {
        cout << "\nGuess a number between 1 and 10: ";
        cin >> guess;
        tries++;//

        if (guess > num)
            cout << "\nSorry, try again\n";
        else if (guess < num)
            cout << "Sorry, try again";
        else
            cout << "Correct! The number was " << guess;
    } while (guess != num);
    return 0;
}

Output 1:

Guess a number between 1 and 10: 3
Sorry, try again
Guess a number between 1 and 10: 6
Sorry, try again
Guess a number between 1 and 10: 9
Sorry, try again
Guess a number between 1 and 10: 10
Sorry, try again
Guess a number between 1 and 10: 1
Sorry, try again
Guess a number between 1 and 10: 2
Sorry, try again
Guess a number between 1 and 10: 4
Sorry, try again
Guess a number between 1 and 10: 5
Sorry, try again
Guess a number between 1 and 10: 7
Correct! The number was 7

Output 2:

Guess a number between 1 and 10: 3
Sorry, try again
Guess a number between 1 and 10: 1
Sorry, try again
Guess a number between 1 and 10: 5
Sorry, try again
Guess a number between 1 and 10: 9
Correct! The number was 9

Output 3:

Guess a number between 1 and 10: 2
Correct! The number was 2



Aucun commentaire:

Enregistrer un commentaire