mercredi 4 avril 2018

How can I generate a random number which is, higher or lower than the random number generated before?

/*Instead of increment 1 or decrease 1 the random number in the for loop, I would like the code to generate a random integer higher than the previous random generated if guess is higher and to generate a random integer lower than the previous random generated if the guess is lower./

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

using namespace std;

int main(){
    int counter = 0;
    unsigned int guess;
    srand(time(NULL));
    unsigned int random_number = (rand() % 100)+ 1;

    std::string higher = "higher";
    std::string lower = "lower";
    std::string value = "";

    cout << "Please enter a number between 1 and 100, so the computer can guess it!" << endl;
    cin >> guess;
    cout << random_number << endl;

    while (guess != random_number)
    {
        cout << "Is it a lower or higher value?" << endl;
        cin >> value;
        if (value == higher)
        {
            for(int i = 1; i< 2; i++)
            {
                random_number = random_number + 1;
                cout << random_number << endl;
            }
        }
        else if (value == lower)
        {
            for(int i =1; i < 2; i++)
            {
                random_number = random_number - 1;
                cout << random_number << endl;
            }
        }
    }
    cout << "This PC is fabulous" << endl;

    return 0;
}




Aucun commentaire:

Enregistrer un commentaire