Question: Topic: Sentinel, Nested loops ( Chapter 5 ) , if / else ( Chapter 4 ) , random numbers ( Chapter 3 ) Do not

Topic: Sentinel, Nested loops (Chapter 5), if/else (Chapter 4), random numbers (Chapter 3)
Do not use anything beyond chapter 5
Write a C++ program that implements a simple number guessing game with multiple questions / answers. For each game, the program generates a random number between 1 and 10. User enters an answer with a numeric input. If the user input number matches the generated number, then print a message to inform users that he/she has a correct guess. If the guess is not correct, allow the user to have two more chances to guess the correct number.
At any time, if users enter 0, then the program should display the game session summary and exits.
The program should keep track of the wins and losses and print the summary counts when the user chooses to exit by entering 0.
The program should generate a new random number only after the user enters the correct guess or after the user has tried 3 times and did not have the right guess. Do not ask the user for a yes/no confirmation after each game because the 0 input value will serve as the sentinel (Chapter 5 topic) to stop the continuous game.
Here is a sample run using the command line.
At the program start, it shows:
Welcome to the number guessing game.
For each game, you have at most 3 chances to guess a secret number from 1 to 10.
The first time, when a new question is asked, the program displays:
Enter a number from 1 to 10. Enter 0 to exit:
When users give a wrong guess, it shows:
Not correct, try again:
When users give a wrong answer after the third trial for the same question, the program displays:
Not correct. You have reached your third trial. The correct number is X.
Let's start a new game
When users answer with the correct number, it shows this prompt:
Congratulations, correct! Lets start a new game.
When users hit 0, the game summary is displayed:
Game summary:
Total games:5
Total game wins: 3
Total game losses:2
Notes:
Use the function rand() to generate random numbers.
Also use time and use the srand() to generate a random seed for the random generator. The srand() should be called only once outside the loop before the game starts.
In main(), you can use a while or "do" loop that gets inputs.
To control the number of maximum trial answers per question, you should use a nested loop inside the main while loop. Using 2 nested loops is fine.
Feel free to use boolean flag variables to control the loops.
Feel free to use logical expression (&&||) in Chapter 4
Generate a new random number only when a new game starts after users either have entered the correct answer or users have exceeded the 3 trials.
For the count of losses, update the count only when users have exceeded the maximum 3 trials. Do not count as a loss if users have not finished the 3 trials.
If you use "break" / "continue" inside loop, there will be deduction of points
7. Add Comment at the top of your program:
/***************************************
/* Author:
/* Description:
/*
/***************************************
8. You can download a zip file that contains sample game by clicking here
How to run the game:
-Unzip the file and save to a folder c:\temp on a Windows PC. Game executable file does not work on Mac.
-Open the Windows command line tool from the Start menu - Run-Type command cd to change to the directory \temp
cd \temp
-Check that your file is in the directory by using the dir command
Welcome to the number guessing game!
For each game, you have at most 3 chances to guess a secret number from 1 to 10.
Enter a number from 1 to 10. Enter 0 to exit: 1
Congratulation, correct! Let's start a new game.
Enter a number from 1 to 10. Enter 0 to exit: 2
Not correct, try again: 3
Congratulation, correct! Let's start a new game.
Enter a number from 1 to 10. Enter 0 to exit: 4
Not correct, try again: 5
Not correct, try again: 6
Not correct. You have reached your 3 trials. The correct number is 2
Lets start a new game.
Enter a number from 1 to 10. Enter 0 to exit: 0
Here is your game summary:
Total games: 3
Total wins: \(\quad 2\)
Total losses: 1
Topic: Sentinel, Nested loops ( Chapter 5 ) , if

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 Programming Questions!