Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Topic: loops, if/else c++ Write a C++ program CIS22A-HW5-XXXXX.cpp that implements a simple number guessing game with multiple questions / answers. For each game, the

Topic: loops, if/else

c++

Write a C++ program CIS22A-HW5-XXXXX.cpp 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 from 1 and 10. 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 counts when user chooses to exit by entering 0.

The program should generate a new random number only after user enters the correct guess or after user has tried 3 times and did not have the right guess. Do not ask user for a yes/no confirmation after each game because the 0 input value will serve as the sentinel to stop the continuous game.

Here is a sample run using 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 trials for the same question, the program displays:

Not correct. You have reached your third trials. The correct number is X.

Lets start a new secret number

When users answer with the correct number, it shows this prompt:

Congratulation, correct! Lets start a new secret number.

When users hit 0, the game summary is displayed:

Here is your 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.h 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 loop that gets inputs.

To control the number of maximum trial answer per question, you should use a nested loop inside the main while loop.

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.

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

Upgrading Oracle Databases Oracle Database New Features

Authors: Charles Kim, Gary Gordhamer, Sean Scott

1st Edition

B0BL12WFP6, 979-8359657501

Students also viewed these Databases questions

Question

Find the derivative. f(x) 8 3 4 mix X O 4 x32 4 x32 3 -4x - x2

Answered: 1 week ago

Question

5. What information would the team members need?

Answered: 1 week ago

Question

Where those not participating, encouraged to participate?

Answered: 1 week ago

Question

Were all members comfortable brainstorming in front of each other?

Answered: 1 week ago