Question: Write a program to play a simple game. You need to generate a random number (integer) by setting a upper limit and a lower limit

Write a program to play a simple game. You need to generate a random number (integer) by setting a upper limit and a lower limit to generate the number. You need to declare the lower limit, and upper limit, and get user input to get two integers. Make sure upper limit is higher than the lower limit, if not, ask the user to enter the numbers again.

Now to generate the random number use the following.

srand(time(0)); // this will generate a seed

random = lowerLimit + rand( ) % (upperLimit lowerLimit + 1)

Now ask the user to guess a number and store in number

Write a do-while statement, and check inside the loop, if the guessed number is same as random, then display the guessed number and the random number, and display the message you win, otherwise ask the user to enter another guess. The while loop is continued until random is equal to the guessed number, i.e. your loop condition should be number != random

You need two additional header files: cstdlib (for using the function rand ) and ctime for using the function time(0).

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!