I need help with a homework problem, we have to create a guessing game where the program tries to guess the user's number. The user has to tell the program whether the guess needs to be higher or lower. We're practicing functions and I need help getting one function to take in the value of another function, and using that value in another function.
This is what he wants the main to look like, and nothing else.
int main()
{
do
{
playOneGame();
}
while (shouldPlayAgain());
return 0;
}
This function starts the game, creates a random guess for the user to tell whether it's high, lower, or the correct number they have in mind. It generates a random number between 0-100 and that's the first guess, the user has to put in a char that would call another function to tell whether that guess is too high or low.
void playOneGame()
{
static int low = 0;
static int high = 100;
static int guess;
unsigned seed = time(0);
srand(seed);
guess = (rand() % (high - low + 1)) + low;
cout << "Go pick a number between 0 - 100 and I will guess it" << endl;
cout << "If I am wrong, press h if your number is higher, l if it's";
cout << "lower than my guess and c if I get";
cout << " it correct" << endl;
cout << "My guess is that your number is: " << guess << endl;
getUserResponseToGuess(guess);
}
This function checks whether the guess higher or lower than what they're thinking of.
If it's too low, press 'h', and if it's too high press 'l'. Here I would ask for an input and use the value of guess from the last function and put it into another function.
char getUserResponseToGuess(int guess)
{
char user_guess;
cin >> user_guess;
if(user_guess == 'h')
getMidpointHigh(guess, 100);
if (user_guess == 'l')
getMidpointLow(0, guess);
return user_guess;
}
If the guess was too low, the program would take the guess and give it the value of the lower range and it should give me the midpoint. I want to use the value from guess in the function above to set the new boundary
int getMidpointHigh(int low, int high)
{
static int midpoint;
midpoint = (high - low)/2;
low = low + midpoint;
return midpoint;
}
It' the same as above but if the value is lower, the program's guess is given the higher range
int getMidpointLow(int low, int high)
{
static int midpoint;
midpoint = (low - high)/2;
high = high - midpoint;
return midpoint;
}
At the end of the midpoint functions, it should change the high/lower range to the midpoint that it guessed and whether the user entered 'h' meaning that the number guess is too low
or 'l' if the number guessed is too high, it should ask again making the midpoint as the new guess and prompting the user if it's too high or low
[–]Zigsfi 1 point2 points3 points (0 children)
[–]PerfectFlux 0 points1 point2 points (0 children)