Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ You will write a program that will play a letter guessing game. General Requ

ID: 3670372 • Letter: C

Question

C++

You will write a program that will play a letter guessing game. General Requirements Write a program that assigns four letters (i.e. char) to four different variables. Your program will ask the user to enter the number of games they wish to play (1 to 4) Your program use one character at a time to play the game and repeat this for the number of games the user wants to play. Maximum number of guesses for each game is 5 Each time the user’s guess is wrong, you should provide him with a hint indicating whether the letter come before or after the letter he/she is trying to guess.(alphabetically) If the player has used up all of their guesses, a message that the game is over should be displayed along with the letter they were trying to guess. When the number of games has been played, the program will end Additional Requirements: Be sure to comment your code adequately. Be sure to indent properly. Check your textbook to see how it should be done. Use meaningful variable names Sample Output: Welcome to the Letter Guessing Game You will enter the number of games you want to play (1 - 4 games) You have 5 chances to guess each letter Let's begin: -------------------------------- How many games do you want to play (1-4) 3 ************************************ Let's play game 1 Enter a guess: e the letter you are trying to guess comes before e Enter a guess: c the letter you are trying to guess comes before c Enter a guess: a You guessed it!!! ************************************ Let's play game 2 Enter a guess: t the letter you are trying to guess comes before t Enter a guess: a the letter you are trying to guess comes after a Enter a guess: p the letter you are trying to guess comes before p Enter a guess: n the letter you are trying to guess comes before n Enter a guess: g the letter you are trying to guess comes after g You did not guess the letter. It was m ************************************ Let's play game 3 Enter a guess: g the letter you are trying to guess comes after g Enter a guess: m the letter you are trying to guess comes after m Enter a guess: t the letter you are trying to guess comes before t Enter a guess: r the letter you are trying to guess comes before r Enter a guess: s the letter you are trying to guess comes before s You did not guess the letter. It was n Press any key to continue . . .

Explanation / Answer

'srand' is used to seed random generator with different seed. Otherwise default 'seed' is taken and each time same random no. are generated!

code is given below, successfully run in Windows environment!

# include <iostream>
# include <stdlib.h> //for 'rand', 'srand'
# include <time.h>   //for 'system time'
using namespace std;

int main()
{
int no; //saves no. of games to play
char c[4]; // saves 4 different characters to guess

cout<<"*****Welocome to letter guessing game***** You will enter the no. of games you want to play(1-4) You have 5 chances to guess each letter Let's begin ";

/*this 'infinite for' runs till user enters the right choice for no. of games to play i.e. between 1-4*/
for(;;){
cout<<" How many games you want to play(1-4): ";
cin>>no;
if(no>4 || no <1) {cout<<" Wrong entry "; continue;}
else break;
}

/*this for loop runs 'no' times to generate random variables and stores them in 'c[]'*/
for(int i=0;i<no;i++)
{
srand(time(NULL)+i);//seeding random generator with system time

int r=97 + ( rand() % ( 122 - 97 + 1 ) );
/*random no. generation withingh range 97-122 i.e. range for ASCII values of a-z*/

c[i]=r; //store character corresponding to 'r' in array

cout<<" "<<c[i];
}



// for loop circulates for no. of games user want to play i.e. 'no'
for(int i=0;i<no;i++)
{
char ch;
cout<<" Lets play game "<<i+1;

int chances=5; //chances are 5 for each game

while(chances>0)
{
   //take user guess as 'ch'
   cout<<" Enter a guess: ";
    cin>>ch;
   
    //if guess is right then print messege and break from loop
    if(ch==c[i]) { cout<<" You guessed it!!! "; break;}
  
    //else give hint and continue the loop with deceremented 'chances'
   else{
    cout<<" ";
    if(ch<c[i]) cout<<"A letter you are trying to guess comes after "<<ch;
    if(ch>c[i]) cout<<"A letter you are trying to guess comes before "<<ch;
   
    chances--;
   }
}

/*if above 'while' loop is stopped due to vanishing of chances then print 'failure' messege*/
if(chances==0)cout<<" You didnot guess the letter! It was "<<c[i];

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote