Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

you must correctly implement the following function in the template code: bool play_game(int n); When I invoke this method, I will supply it an integer

you must correctly implement the following function in the template code:

bool play_game(int n);

When I invoke this method, I will supply it an integer parameter, n, which you must treat as a secret number.

Then you must give the user 6 chances to guess the number using the exact script below: As soon as the function begins, print the following on the console to greet the user:

Welcome to my number guessing game

followed by exactly two blank newlines.

Then your function must loop at most six times. During each iteration it must ask the user to guess the secret by printing the following exact prompt:

Enter your guess:

Note - there is one space following the colon at the end of the prompt. You must not end the prompt with a newline.

Example template:

#include #include 
using namespace std; 

// Give the user 6 chances to guess the secret number n (0-10). If they get it, // say so and return true. Else say so and return false. bool play_game(int n) {

// TODO - Your code here

}

outcome solution should look like this:

 Welcome to my number guessing game Enter your guess: You entered: 6 Enter your guess: You entered: 8 Enter your guess: You entered: 1 Enter your guess: You entered: 5 Enter your guess: You entered: 4 Enter your guess: You entered: 10 
I'm sorry. You didn't find my number. or
You found it in 3 guess(es). 

More details

Now you will compare the user's guess to your secret number. If they are equal, you must return the value true to the caller after printing the following message to the console:

You found it in N guess(es). where you would replace the N with the actual number of guesses it took the user to find the

number. This should be followed by exactly one newline.

If the user's guess is not equal to the secret number, you must simply loop back to the top and repeat the whole sequence.

Except, of course, if you've already done it six times. In that case, you must print a blank line, and the following message (two lines) on the console. Then return false to your caller.

I'm sorry. You didn't find my number. It was SECRET

You should replace SECRET with the actual secret number. This shouldn't require more than 20-25 lines of code total (to give you a rough idea of when

you're unknowingly over-coding). Refer to the provided code template for more information.

Important: Your user input mechanism must be robust. I may try and break it by inputting a string when an integer is expected. In this case, your program should treat it as a 0, rather than break out of the logic.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

4. Use in-store wrapping services.

Answered: 1 week ago