Question
Write a C++ program to simulate a character-guessing game. The program randomly generates a lower case character (between a and z) and asks the user
Write a C++ program to simulate a character-guessing game. The program randomly generates a lower case character (between a and z) and asks the user for the maximum number of guesses or attempts. The user is then asked to guess the character inside a loop. The program will inform the user if the guess is low or high. The loop will terminate if the user guessed the character.
After the loop, the program prints out the final result.
Example Run (the char to guess was p in this example):
Enter the max number of guesses: 7
Attempt: 1 | Enter your char guess: l
Your guess is less than the hidden char!
Attempt: 2 | Enter your char guess: r
Your guess is greater than the hidden char!
Attempt: 3 | Enter your char guess: o
Your guess is less than the hidden char!
Attempt: 4 | Enter your char guess: p
Result AFTER the loop:
You guessed the char p after 4 attempts!
Press any key to continue . . .
Example Run (the char to guess was x in this example):
Enter the max number of guesses: 3
Attempt: 1 | Enter your char guess: z
Your guess is greater than the hidden char!
Attempt: 2 | Enter your char guess: h
Your guess is greater than the hidden char!
Attempt: 3 | Enter your char guess: m
Your guess is greater than the hidden char!
Result AFTER the loop:
You did not guess the char x after 3 attempts!
Press any key to continue . . .
---------------------------------------------------------------
The loop terminates if:
- The use finished all attempts without guessing
- Or, the user managed to guess the character
Here is a skeleton program that you can use, feel free to add, modify or remove variables if needed:
#include
using namespace std;
int main()
{
//Suggested variables, feel free to add/delete/modify variables as you need.
char toGuess;
//Generate a random number between the ASCII code of a which is 97 and the //ASCII code of z which is 122. To generate a random number between two values, //you can use rand()%(max-min+1)+min. Then cast the generated integer to the type //char and store it in toGuess.
char fromUser; //user's guess is stored here; cin >> fromUser
int attempt = 0; // a counter for the number of attempts for the loop
int maxNumOfGuesses; // read this from the user at the beginning of the program
bool guessed = false; //did the user guess it yet? Store the result here
do {
cout << "Enter the max number of guesses: ";
cin >> maxNumOfGuesses;
} while (maxNumOfGuesses <= 0);
//add your loop here
cout << " Result AFTER the loop: ";
//add your final result here
//(you are NOT allowed to print the final result from within the loop)
//Ex. You guessed the char p after 4 attempts!
system("pause");
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started