Share
Explore BrainMass

# Guessing Game

Write a program in C++ to play a guessing game with the user. The program should be able to make a guess about the chosen number by the user and ask whether the guessed number is above or below the chosen number. If the guess is correct the program should report the number of tries and the correctly guessed number.
As an advanced feature run the game 10 times and determine the average number of tries for the successful guess.

Print out for each game, the number the computer generated, the number of guesses made to guess the number.
The last piece of information to print out is the average number of guesses.

#### Solution Preview

Please find the source code attached with included explanations in the comments. The game runs 10 times using a for-loop. For each iteration of the for-loop, a do-while loop continuously check user guesses against the predetermined random number generated before each game. The tries for each game (including the successful one at the last) are accumulated in a tot_guesses variable to calculate final average value for number of tries, which is
total number of tries/total number of games.
Following is a sample output generated when 5 games are played. Note the attached code has total games = 10 (as per the requirement).

\$ g++ Untitled1.cpp
\$ ./a.out
Game number: 1
Guess My Number Game
_______________________

Enter ...

#### Solution Summary

C++ program that plays a guessing game with the user. It tries to guess a number chosen by the user. To correctly guess the number, the program asks questions. If it guesses correctly the program prints the solution and the number of tries. In case of multiple games, it reports the average number of tries.

\$2.19