Here's my current code and I am stuck completing my playOneGame(). Could someone please guide me towards the right path?
A sample run of the program might look like this, with the user inputs below:
/** Sample Code **/
Think of a number between 1 and 100.
Is it 50? (h/l/c): h
Is it 75? (h/l/c): h
Is it 88? (h/l/c): l
Is it 81? (h/l/c): c
Great! Do you want to play again? (y/n): y
Think of a number between 1 and 100.
Is it 50? (h/l/c): l
Is it 25? (h/l/c): h
Is it 37? (h/l/c): c
Great! Do you want to play again? (y/n): n
#include <iostream>
usingnamespace std;
/***** Function declaration (prototype) *****/
void playOneGame();
char getUserResponseToGuess(int);
int getMidpoint(int, int);
bool shouldPlayAgain();
/***** main function *****/
int main() {
do {
playOneGame();
} while (shouldPlayAgain());
return 0;
}
/***** Function definition (header and body) *****/
// The playOneGame() function has a return type of void. It implements a complete guessing game on the range of 1 to 100. getUserResponseToGuess(), and getMidpoint() should be called inside the playOneGame()
void playOneGame()
{
int low = 1;
int high = 100;
int previous;
int num;
char response;
cout << "Think of a number between 1 and 100." << endl;
response = getUserResponseToGuess(int guess);
while (response != 'c')
{
if (response == 'l')
{
// calculate midpoint
num = midpoint(guess, high);
previous = guess;
}
elseif (response == 'h')
{
// calculate midpoint
num = midpoint(guess, high);
previous = guess;
}
else
{
shouldPlayAgain();
}
}
// This function prompts the user with the phrase “is it <guess>? (h/l/c): “ with the value replacing the token <guess> and returns a char. The char should be one of three possible values: ‘h’, ‘l’, or ‘c’.
char getUserResponseToGuess(int guess)
{
char input;
cout << "Is it " << guess << "? (h/l/c): ";
cin >> input;
}
// This function accepts two integers, and returns the midpoint of the two integers.
int getMidpoint(int low, int high)
{
int midpoint;
midpoint = ((low + high)/2);
return midpoint;
}
// The shouldPlayAgain() function has a bool return type. It prompts the user to determine if the user wants to play again, reads in a character, returns true if the character is a ‘y’, and otherwise returns false.
bool shouldPlayAgain()
{
char again;
bool status;
cout << "Would you like to play again" << endl;
cin >> again;
if (again == 'y' || 'Y') {
bool status = true;
} else {
bool staus = false;
}
return status;
}
What exactly do you need guidance with? In a guessing game like the one you're creating, you'll need a way to generate a random number. You can do this by seeding the random number generator and then use rand() or mt19937 (mersenne twister engine) to obtain a random value from 1 to 100. You can use either one, use mt19937 if you need a reasonable level of unpredictability. Though, for a simple guess my number game, rand() should do.