Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a C++ program main.cpp that implements a simple number guessing game with multiple questions / answers. For each game, the program generates a random

Write a C++ program main.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 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 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 (Chapter 5 topic) 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:

Game summary:

Total games: 5

Total game wins: 3

Total game losses: 2

Notes:

  1. Use the function rand() to generate random numbers.

  2. 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.

  3. In main(), you can use a while loop that gets inputs.

  4. To control the number of maximum trial answer per question, you should use a nested loop inside the main while loop. Using a nested "for" loop is fine.

  5. 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.

  6. 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.

  7. You can use logical operators (AND, OR, NOT) to simplify code.

8. Add Comment at the top of your program:

/***************************************

/* Author:

/* Description:

/*

/***************************************

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

What Is A Database And How Do I Use It

Authors: Matt Anniss

1st Edition

1622750799, 978-1622750795

More Books

Students also viewed these Databases questions