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;
}
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 . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.