In: Computer Science
C++. Write a program that generates a random number between 5 and
20 and asks the user to guess what the number is. If the user’s
guess is higher than the random number, the program should display
Too high. Try
again. If the user’s guess is lower than the random
number, the program should display Too
low, Try again. The program should use a
loop that repeats while keeping a count of the number of guesses
the user makes until the user correctly guesses the random number.
Then the program should display the number of guesses along with
the following message Congratulations. You figured out
my number.
The algorithm should provide the user the opportunity to play the game again or quit.
This criterion is linked to a Learning OutcomeCorrect implementation of random number in range of 5 to 15
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int randomnum, userguess, count=0;
srand(time(0));
randomnum = rand() % (20-5)+5; //random number between
5 and 20
cout << "Enter your guess :"<<endl;
do
{
cin>>userguess;
count++;
if
(userguess>randomnum)
cout <<
"Too high. Try again"<<endl;
else if
(userguess<randomnum)
cout <<
"Too low. Try again"<<endl;
else
{
cout
<<"Congratulations. you figured out my
number"<<endl;
cout<<"The
number of guesses is :"<<count;
}
} while (userguess!=randomnum);
return 0;
}