I'm doing some practice questions to learn how to program in C... It's essentially a guessing game program. "Have the program initially guess 50, and have it ask the user whether the guess is high, low, or correct. If, say, the guess is low, have the next guess be halfway between 50 and 100, that is, 75. If that guess is high, let the next guess be halfway between 75 and 50, and so on." We're assuming that the user won't cheat. I'm rusty so I can't remember what operators I should use to increment the program's response based on the user's input. It'd be 2x the number, or half the number based on a response of H or L. Anyone know? I know it's easy, I just cant figure it out.
#include <stdio.h>
#include <ctype.h>
int main(int argc, const char * argv[])
{
int low;
int high;
int guess;
int response;
int toupper ( int );
do
{
low = 1;
high = 100;
guess = 50;
printf("Pick an integer from 1 to 100 and I will try to guess it.\n");
printf("If I guess too low, respond with a L.\n");
printf("If I guess too high, respond with a H.\n");
printf("If I guess correctly, respond with a Y.\n");
printf("Okay, is your number %d?\n",guess);
while( (response=getchar()) != 'y')
{
if( response == 'l' )
{
// TODO: Ajust the value of low
// TODO: Ajust the value of guess
printf("Too low eh...well then, is it %d?\n",guess);
}
else if( response == 'h' )
{
// TODO: Ajust the value of high
// TODO: Ajust the value of guess
printf("Too high eh...well then, is it %d?\n",guess);
}
else if( response != '\n' )
{
fflush(stdin);
printf("Sorry, I only understand L, H and Y. Please try again.\n");
}
}
printf("Please enter R to restart, or Q to quit.\n");
response=getchar();
while ((response != 'q') && (response != 'r'))
{
if (response != '\n')
{
fflush(stdin);
printf("Oops! Please enter R or Q.\n");
}
response=toupper(getchar());
}
} while (response=='R');
return 0;
}
I'm struggling with what to put in where the hidden notes are.
Any insight would be appreciated!