Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You will write a simple “Guess My Number” game. The program will ask the user to

ID: 3628734 • Letter: Y

Question

You will write a simple “Guess My Number” game. The program will ask the user to guess a number between 1 and 100. As the user guesses, the program will indicate whether the number is higher or lower than the guess.

When the user guesses the number correctly, the program will tell the user how many guesses it took them to guess the number.

an example:
$ ./guess_my_number
I am thinking of a number between 1 and 100.

Guess my number: 50
My number is less than 50.

Guess my number: 25
My number is higher than 25.

Guess my number: 37
My number is less than 37.

Guess my number: 32
My number is less than 32.

Guess my number: 28
My number is higher than 28.

Guess my number: 30
My number is higher than 30.

Guess my number: 31
You guessed my number in 7 guesses.

Thanks for playing.
$

Use a constant for the upper limit
Declare a constant for the UPPER_LIMIT of the guessing game. This way if in the future you wanted to change the range to a number between 1 and 1000, you would just need to change the value for the constant. Remember, to declare a constant, we would do the following:

const int UPPER_LIMIT = 100;
Then instead of using the value ‘100’ throughout your code, you would refer to the constant UPPER_LIMIT.

Set your random number to 31
Recall that the testing of your program is done automatically by the turnin system. As a result, in order to test your program, we all have to have the same random number. So, in your program, make your random number equal to 31.

Explanation / Answer

#include<iostream>
using namespace std;
const int UPPER_LIMIT = 100;
const int LOWER_LIMIT = 1;
int main()
{
int random_number=31;
int count =1;
int guess;
cout <<" Guess my number: ";
cin >> guess;
while(guess!=random_number)
{
if(random_number > guess)
cout << "My number is higher than "<< guess << endl;
if(random_number < guess)
cout << "My number is less than "<< guess << endl;
count++;
cout <<" Guess my number: ";
cin >> guess;

}
cout << " you guessed my number in " << count << " guesses" << endl;
return 0;
}


Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote