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;
}
}