-1

I am trying to create a game where a user enters a number to be the maximum number of an interval, then the program asks the user to guess a random number. When I test it out, the user can keep guessing but there is no random number that ends up being correct.

int main() {
    int num, intervalMax, guess; // Define variables
    srand(time(0)); // Give a "random" seed to the random number generator

    cout << "Let's generate some random numbers in the interval [1, 10]" << endl;
    for (int i = 0; i < 5; i++) {
        num = rand() % 10 + 1;
        cout << "Your random number is " << num << "!" << endl;
    }

    cout << endl;
    cout << "Great, now it's time to play a guessing game." << endl;
    cout << "What should the maximum number we can generate be?" << endl;
    cout << "The wider the interval, the harder the game is." << endl;
    cout << "Please enter a number: ";
    cin >> intervalMax; // ask for the maximum number allowed
    cout << "Cool, the numbers we generate will be in the interval [1, " << intervalMax << "]" << endl;

    cout << "\n...generating a new random number...\n" << endl;
    for (int i = 0; i < 5; i++) {
           num = rand() % intervalMax + 1;
    
    cout << "Now, you are the player. Time to make a guess. What might the random number be?" << endl;
        
      
        
        //ask for estimate
        
        do
        {
            num = rand() % 20 + 1;
            cout << "Enter your guess: ";
        cin >> guess;
            i++;
            
            
        if (guess>num)
            cout << "Your guess is more than the random number." <<endl;
       else if (guess<num)
           cout << "Your guess is less than the random number"<< endl;
            else if (guess ==num)
                cout << "Your guess is correct." <<endl;
        }
        while (guess !=num);
        
      
    return 0;

}
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
  • 1
    What did you observe when stepping through your code line by line with the debugger? – πάντα ῥεῖ Oct 13 '20 at 17:20
  • 2
    Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine at which point your program stops behaving as intended? If you did not try this, then you probably want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). Even if this does not solve your problem, it should at least help you produce a [mre] of your problem. – Andreas Wenzel Oct 13 '20 at 17:24
  • 2
    You generate a new random number in the loop, so each time the user makes a guess, it's a new number. – blacix Oct 13 '20 at 17:28
  • The line `cin >> intervalMax;` may fail, for example if the user enters a letter instead of a number. You should check [`cin.fail()`](https://en.cppreference.com/w/cpp/io/basic_ios/fail) before proceeding after that line. – Andreas Wenzel Oct 13 '20 at 17:30

2 Answers2

6

You are assigning variable num twice

num = rand() % intervalMax + 1;

num = rand() % 20 + 1;

I suppose that you should delete second line

zolty13
  • 1,943
  • 3
  • 17
  • 34
  • 1
    Just as a side note for the OP: This problem could have been easily found with the help of a [debugger](https://stackoverflow.com/q/25385173/12149471), by running the program line by line and monitoring the values of all variables (including the variable `num`). It would have been clearly visible in the debugger that the variable `num` is changing. – Andreas Wenzel Oct 13 '20 at 17:42
0
#include <iostream>

using namespace std;

int main() {
    int num, intervalMax, guess; // Define variables
    srand(time(0)); // Give a "random" seed to the random number generator

    cout << "Let's generate some random numbers in the interval [1, 10]" << endl;
    for (int i = 0; i < 5; i++) {
        num = rand() % 10 + 1;
        cout << "Your random number is " << num << "!" << endl;
    }

    cout << endl;
    cout << "Great, now it's time to play a guessing game." << endl;
    cout << "What should the maximum number we can generate be?" << endl;
    cout << "The wider the interval, the harder the game is." << endl;
    cout << "Please enter a number: ";
    cin >> intervalMax; // ask for the maximum number allowed
    cout << "Cool, the numbers we generate will be in the interval [1, " << intervalMax << "]" << endl;
    cout << "\n...generating a new random number...\n" << endl;
    cout << "Now, you are the player. Time to make a guess. What might the random number be?" << endl;
    
    for (int i = 0; i < 5; i++) {
        num = rand() % intervalMax + 1;
        
        //ask for estimate
        do
        {
            cout << "Enter your guess: ";
            cin >> guess;
            
            if (guess > num)
                cout << "Your guess is more than the random number." << endl;
            else if (guess < num)
                cout << "Your guess is less than the random number." << endl;
            else if (guess == num)
                cout << "Your guess is correct. Next number is? ..." << endl;
        }
        while (guess !=num);
    }
    return 0;
}
Let's generate some random numbers in the interval [1, 10]
Your random number is 10!
Your random number is 6!
Your random number is 10!
Your random number is 3!
Your random number is 7!

Great, now it's time to play a guessing game.
What should the maximum number we can generate be?
The wider the interval, the harder the game is.
Please enter a number: 3
Cool, the numbers we generate will be in the interval [1, 3]

...generating a new random number...

Now, you are the player. Time to make a guess. What might the random number be?
Enter your guess: 2
Your guess is more than the random number.
Enter your guess: 1
Your guess is correct. Next number is? ...
Enter your guess: 2
Your guess is more than the random number.
Enter your guess: 1
Your guess is correct. Next number is? ...
Enter your guess: 3
Your guess is more than the random number.
Enter your guess: 2
Your guess is correct. Next number is? ...
Enter your guess: 3
Your guess is correct. Next number is? ...
Enter your guess: 3
Your guess is more than the random number.
Enter your guess: 2
Your guess is correct. Next number is? ...
...
// end of main
777moneymaker
  • 697
  • 4
  • 15
  • 1
    What's the point of this answer? FYI, the point of OP's learning is to provide the OP with minimal code so that the OP can learn on their own. People only learn that you will provide them with all their code, without having them learn how to program. – Thomas Matthews Oct 13 '20 at 18:16