vendredi 29 juillet 2016

C++ Random Number Generator

I'm very new to programming and have just started learning C++ over the last day or two.

Im trying to make a small program in which the computer guesses a number the user has thought of.

I've got a loop that generates a random number between 0-100. What I'm trying to achieve is if the user inputs 'too high', the program will loop and generate a new number lower than its previously generated number. Same if the user inputs 'too low', the program will loop and generate a new number higher than its previously generated number.

I just cannot figure out what to write to get it to generate a new number higher or lower than the previously generated number.

For example, if the computer generates the number 60 and the user inputs 'high', the next random number should be between 0 and 59.

Any help would be greatly appreciated!

    srand(time(NULL));
    int rndNumber = rand() % 100 + 1;
    string userHighLow;

    cout << "Think of a random number between 0-100 and the computer will try and guess!" << endl;
    cout << "Use the keywords 'High' and 'Low' to help the computer guess." << endl;


 do {   
    cout << "My guess is: " << rndNumber << endl;

    cin >> userHighLow;

    if (userHighLow == "High" || userHighLow == "high")
    {
        //Unsure how to write new rand number lower than previous

    } else if (userHighLow == "Low" || userHighLow == "low")
    {
        //Unsure how to write new rand number higher than previous
    }
 }
    while (userHighLow != "Correct" || userHighLow == "correct");
    return 0;
}




Aucun commentaire:

Enregistrer un commentaire