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

# include # include //for \'rand\', \'srand\' # include //for \'system time\' us

ID: 3675387 • Letter: #

Question

# include # include //for 'rand', 'srand' # include //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;i0) { //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(chc[i]) cout<<"A letter you are trying to guess comes before "<

form the program that was written to play a letter guessing game (Project 1) to a function-based program.
General Requirements for project 2
Rewrite the program from project 1 using at least four programmer-defined functions other than the main function.
All functions (except the main function) will be defined as follows:
o The function prototype appears before the main
o Comment describing what the function does appears directly under function prototype
o Formal variable names are not used in any prototype
o The functions definition appears after the main
The first function’s name is introduction, which will print (show on the screen)
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:
--------------------------------
The second function’s name is getNumberOfGame, which asks the user about the number of games to be played and returns the value. This function will validate the user input to make sure it is in the desired range (1-4) inclusive, if the value is out of range the user is notified and asked again.
How many games do you want to play (1-4) 3
The third function’s name is compareTwoCharacters, which receives two parameters of type character (e.g. the character to be guessed in that game and the character typed by the user). This functions return 0 if the two characters are the same, returns 2 if the first parameter comes after the second parameter, otherwise it returns -2.
The fourth function’s name is playOneGame, which receives one parameter of type character (the character to be guessed in that game). This function returns true if the user won; otherwise will return false. This function will handle all tasks to play one game (ask the user to enter their guess, provide up to 5 chances to guess, provide a hint to the user, etc.). This function is going to call the third function (i.e. compareTwoCharacters) to decide if the user has guessed the correct answer or to provide a hint to the user.
Make sure to follow the instructions carefully including using the exact function names with the exact letter cases (upper and lower case letters). Your programs are going to be tested using a unified main function that is developed for that purpose. If the unified main function does not compile this indicates inconsistencies in meeting the above requirements and will result in a zero grade.

Explanation / Answer

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

// prototype of functuons
// welcome function, it does not take any param and it does not retuen any thing
void welcome();

// this will ask user about number of games he want to play, no any params, return int value
// this will also validate user input
int getNumberOfGame();

// this function compares two char params and return int value
int compareTwoCharacters(char , char );

//receives one parameter of type character (the character to be guessed in that game).
// This function returns true if the user won; otherwise will return false.
bool playOneGame(char );

int main() {
   int no; //saves no. of games to play
   char c[4] = {'a', 'b', 'c', 'd'}; // saves 4 different characters to guess
  
   // calling welcome method
   welcome();
  
   // taking user input for number of games
   no = getNumberOfGame();
  
   /*this for loop runs 'no' times to generate random variables and stores them in 'c[]'*/
   for(int i=0;i<no; i++) {
       // playing one game in each loop
       bool status = playOneGame(c[i]);
      
       if(status == true ){// if guessed correctly
           cout<<" You guessed it!!! ";
           }
       else{
           cout<<" You did not guessed it!!! Try again ";
           }
   }
   return 0;          
   }
  
// definition of welcome  
void welcome(){
   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 ";
   }
  
// definition of getNumberOfGame
int getNumberOfGame(){
   int no;
       /*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;
   }
   return no;
   }
//definition of compareTwoCharacters
int compareTwoCharacters(char first, char second){
   if(first == second)
       return 0; // equal
   else if(first > second)
       return 2; // first comes before second
   else
       return -2; // first comes after seconds
   }
  
bool playOneGame(char c){
   //take user guess as 'ch'
   char ch;
   cout<<" Enter a guess: ";
   cin>>ch;
   //calling compareTwoCharacters method
   int compare = compareTwoCharacters(ch, c) ;
   if(compare == 0) {
       return true;
       }
       //else give hint and continue the loop with deceremented 'chances'
   else{
       cout<<" ";
       if(compare == 2)
           cout<<"A letter you are trying to guess comes before ";
       else if(compare == -2)
           cout<<"A letter you are trying to guess comes after ";
       }
       return false;
   }


/*

output:

*****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


How many games you want to play(1-4): 2

Enter a guess: b

A letter you are trying to guess comes before

You did not guessed it!!! Try again


Enter a guess: b

You guessed it!!!

*/