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

According to the following C++ code from Visual Studio, rewrite it with more fun

ID: 3724877 • Letter: A

Question

According to the following C++ code from Visual Studio, rewrite it with more function with the request in the image.

#include<iostream>
#include<fstream>
#include<cstdlib>//library includes the definition of rand funcion (a random number generator) and the exit function
#include<ctime>//library includes the the definition of time funcion.
using namespace std;
int main() {
   int NUMBER_OF_GUESSES = 5;
   int numberOfGames = 0;
   int loopCounter;
   char usersGuess, secretLetter;
   bool winFlag = false;
   //ifstream inputFile; //Uncomment for option two read the value from the file
   /* //Uncomment for option two read the value from the file
   inputFile.open("Secret_Letters.txt"); //Uncomment for option two read the value from the file
   if (inputFile.fail()) //Uncomment for option two read the value from the file
   cout<<"File Opening Failed "; //Uncomment for option two read the value from the file
   exit(1); //Uncomment for option two read the value from the file
   } //Uncomment for option two read the value from the file
   */ //Uncomment for option two read the value from the file
   srand(time(0));//seed the random function to generate different random numbers each time
   cout << "Welcome to the Letter Guessing Game ";
   cout << "You will enter the number of games you want to play (1 - 4 games) ";
   cout << "You have " << NUMBER_OF_GUESSES << " chances to guess each letter ";
   cout << "Let's begin: ";
   cout << "-------------------------------- ";
   while (numberOfGames<1 || numberOfGames>4) {
       cout << "How many games do you want to play (1-4)";
       cin >> numberOfGames;
   }
   for (int i = 1; i <= numberOfGames; i++) {
       cout << " ************************************ ";
       //Initiailization before each games include assigneing the secretLetter, resetting the winFlag, and loopCounter
       winFlag = false;
       loopCounter = 0;
       secretLetter = rand() % 26 + 'a';//rand()%26 to generate a number between 0 to 25 inclusive to be added to the letter a to generate a random letter [a-z]
                                       //inputFile>>secretLetter; //Uncomment for option two read the value from the file
                                       //cout<<secretLetter<<endl; //Uncomment to test your code
       cout << "Let's play game " << i << endl;
       while (loopCounter<NUMBER_OF_GUESSES) {
           cout << "Enter a guess: ";
           cin >> usersGuess;
           if (usersGuess == secretLetter) {
               winFlag = true;
               break;
           }
           else if (usersGuess>secretLetter) {
               cout << "the letter you are trying to guess comes before " << usersGuess << endl;
           }
           else {
               cout << "the letter you are trying to guess comes after " << usersGuess << endl;
           }
           loopCounter++;
       }
       if (winFlag) {
           cout << "You guessed it!!! ";
       }
       else {
           cout << "You did not guess the letter. It was " << secretLetter << endl;
       }
   }
   //inputFile.close(); //Uncomment for option two read the value from the file
   system("pause");
   return 0;
}

Guessing game program using functions General Requirements for project 2 Rewrite the program from project 1 using at least four user-defined functions other than the main function All functions (except the main function) will be defined as follows · The function prototype appears before the main o o o Comment describing what the function does appears directly under function prototype Formal variable names are not used in any prototype 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

Hello, I have a solution for you. Implemented everything as per the requirements. Defined following things in this answer.

//updated cpp code

#include<iostream>

#include<fstream>

#include<cstdlib>//library includes the definition of rand funcion (a random number generator) and the exit function

#include<ctime>//library includes the the definition of time funcion.

using namespace std;

/*declaring the number of guesses outside the main method, so that the intro method can use it*/

const int NUMBER_OF_GUESSES = 5;

/*method to display an intro message to the user*/

void introduction();

/*method to prompt the user to enter number of games, and returns it (1-4)*/

int getNumberOfGame();

/*this method will compare two characters, return 0 if they are equal, 2 if first one comes after second, else -2*/

int compareTwoCharacters(char,char);

/*method to conduct one game, with a given mystery character*/

bool playOneGame(char);

int main() {

   

    int numberOfGames = 0;

    int loopCounter;

    char usersGuess, secretLetter;

    bool winFlag = false;

                                      //Uncomment for option two read the value from the file

    srand(time(0));//seed the random function to generate different random numbers each time

   

    introduction();

    /*getting number of games*/

    numberOfGames=getNumberOfGame();

    //loops for the specified number of games

    for (int i = 1; i <= numberOfGames; i++) {

        cout << " ************************************ ";

        secretLetter = rand() % 26 + 'a';//rand()%26 to generate a number between 0 to 25 inclusive to be added to the letter a to generate a random letter [a-z]

        cout << "Let's play game " << i << endl;

        /*playing game, by calling the method playOneGame, it will return true if player won else false*/

        if(playOneGame(secretLetter)){

                //won

                cout << "You guessed it!!! ";

                                }else{

                                                //failed

                                                cout << "You did not guess the letter. It was " << secretLetter << endl;

                                }

    }

   

    system("pause");

    return 0;

}

void introduction(){

                cout << "Welcome to the Letter Guessing Game ";

    cout << "You will enter the number of games you want to play (1 - 4 games) ";

    cout << "You have " << NUMBER_OF_GUESSES << " chances to guess each letter ";

    cout << "Let's begin: ";

    cout << "-------------------------------- ";

}

int getNumberOfGame(){

                int n;

                cout << "How many games do you want to play (1-4) ";

                cin>>n;

                if(n<1 || n>4){

                                //out of range

                                cout<<"Invalid number of games, try again... ";

                                //asking again, recursively

                                return getNumberOfGame();

                }else{

                                //valid, returning it

                                return n;

                }

}

int compareTwoCharacters(char charToBeGuessed,char userGuess){

                if(userGuess==charToBeGuessed){

                                //right guess

                                return 0;

                }else if(userGuess>charToBeGuessed){

                                //second parameter comes after first

                                return -2;

                }else{

                                //first parameter comes after second

                                return 2;

                }

}

bool playOneGame(char secretLetter){

                bool winFlag=false;

                int loopCounter=0;

                char usersGuess;

                /*loops until all guesses are used, or the user guessed it right*/

                while (loopCounter<NUMBER_OF_GUESSES) {

            cout << "Enter a guess: ";

            cin >> usersGuess;

            if(compareTwoCharacters(secretLetter,usersGuess)==0){

                //won

                winFlag = true;

                break;

                                                }else if(compareTwoCharacters(secretLetter,usersGuess)==2){

                                                                cout << "the letter you are trying to guess comes after " << usersGuess << endl;

                                                }else{

                                                                cout << "the letter you are trying to guess comes before " << usersGuess << endl;

                                                }          

            loopCounter++;

        }

        if (winFlag) {

            return true;

        }

        else {

            return false;

        }

}

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

Invalid number of games, try again...

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

************************************

Let's play game 1

Enter a guess: s

the letter you are trying to guess comes before s

Enter a guess: e

the letter you are trying to guess comes before e

Enter a guess: b

the letter you are trying to guess comes after b

Enter a guess: c

the letter you are trying to guess comes after c

Enter a guess: d

You guessed it!!!

************************************

Let's play game 2

Enter a guess: s

the letter you are trying to guess comes before s

Enter a guess: f

the letter you are trying to guess comes after f

Enter a guess: j

the letter you are trying to guess comes after j

Enter a guess: l

the letter you are trying to guess comes after l

Enter a guess: o

the letter you are trying to guess comes after o

You did not guess the letter. It was p

Press any key to continue . . .

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