Hi, /*Starting code for guessing game. Functions introduction, getGuess, and tes
ID: 3839914 • Letter: H
Question
Hi,
/*Starting code for guessing game.
Functions introduction, getGuess, and testGuess need to be completed. Range of numbers to guess needs to be set. Code needs to be added to ask if user wants another game and loop control set accordingly. Code needs to be added to track number of guesses.
*/
#include <iostream>
using namespace std;
void introduction(int high, int low);
int getGuess();
bool testGuess(int guess, int numberToGuess);
int main(int argc, char *argv[]){
int high = 0;
int low = 0;
int numberToGuess;
bool stillPlaying = true;
bool winner = false;
int guess;
while (stillPlaying)
{
// generate a random number between low and high
valuenumberToGuess = rand() % (high - low + 1) + low;
//tell the user about the game
introduction(high, low);
while (!winner)
{
guess = getGuess();
winner = testGuess(guess, numberToGuess);
if (winner)
{
//output a congratulatory message
}
else
;
//output the number of guesses they've made so far
}
//ask the user if they want to play again, and if not, change the loop control condition
}
cout << "Thanks for playing!" << endl;
cin.ignore();
cin.get();
}
//Tells the user the rules of the game
void introduction(int high, int low)
{
//I'm thinking of a number between high and low.....
}
//Prompts for, inputs, and returns the next guess
int getGuess()
{
return 0;
}
// returns true if guess is correct
// if guess is not correct, outputs a high or low message and returns false
bool testGuess(int guess, int numberToGuess)
{
return false;
}
1. Complete the bodies of these functions. Comments for the functions indicate their purpose. void welcome (string name, int high, int low); int get Guess(string name) bool testGuess(int guess, int numberToGuess), 2. Set the high and low range to something other than 0. 3. Add a variable and code to track the number of guesses for each game. Be sure to reset it if a new game is played. 4. Output a congratulatory message if the user guesses the number. 5. Output the current number of guesses if they did not guess the number. 6. Ask if they want to play again, and set the loop control variable appropriately 7. Add a function (for example, void closing0, that is called when they are done playing to close the game with some sort of message to the user- perhaps track and output the number of games played, or just say thanks and goodbye.Explanation / Answer
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void introduction(int high, int low);
int getGuess();
bool testGuess(int guess, int numberToGuess);
void closing();
int main(int argc, char *argv[]){
int high = 10;
int low = 1;
int numberToGuess;
bool stillPlaying = true;
bool winner = false;
int guess;
int numberOfGuess = 0;
char ch;
srand (time(NULL));
while (stillPlaying)
{
numberOfGuess =0;
// generate a random number between low and high
numberToGuess = rand() % (high - low + 1) + low;
//tell the user about the game
introduction(high, low);
while (!winner)
{
guess = getGuess();
numberOfGuess++;
winner = testGuess(guess, numberToGuess);
if (winner)
{
//output a congratulatory message
cout<<"Congratulations. You Win!"<<endl;
}
else{
//output the number of guesses they've made so far
cout<<"The number of guesses you've made: "<<numberOfGuess<<endl;
}
}
//ask the user if they want to play again, and if not, change the loop control condition
cout<<"Do you want to continue: (y o n): ";
cin >> ch;
if(ch =='y' || ch=='Y'){
stillPlaying = true;
winner=false;
}
else{
stillPlaying = false;
closing();
}
}
}
//Tells the user the rules of the game
void introduction(int high, int low)
{
//I'm thinking of a number between high and low.....
cout<<"I'm thinking of a number between "<<low<<" and "<<high<<endl;
}
//Prompts for, inputs, and returns the next guess
int getGuess()
{
int guess;
cout<<"Enter the guess: ";
cin>> guess;
return guess;
}
// returns true if guess is correct
// if guess is not correct, outputs a high or low message and returns false
bool testGuess(int guess, int numberToGuess)
{
if(guess == numberToGuess){
return true;
}
return false;
}
void closing(){
cout << "Thanks for playing!" << endl;
}
Output:
sh-4.2$ main
I'm thinking of a number between 1 and 10
Enter the guess: 4
Congratulations. You Win!
Do you want to continue: (y o n): y
I'm thinking of a number between 1 and 10
Enter the guess: 7
Congratulations. You Win!
Do you want to continue: (y o n): y
I'm thinking of a number between 1 and 10
Enter the guess: 8
Congratulations. You Win!
Do you want to continue: (y o n): y
I'm thinking of a number between 1 and 10
Enter the guess: 9
The number of guesses you've made: 1
Enter the guess: 1
The number of guesses you've made: 2
Enter the guess: 2
The number of guesses you've made: 3
Enter the guess: 3
The number of guesses you've made: 4
Enter the guess: 4
The number of guesses you've made: 5
Enter the guess: 5
The number of guesses you've made: 6
Enter the guess: 6
Congratulations. You Win!
Do you want to continue: (y o n): n
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.