take a look at this. is that even close to what you need?
Code:
#include <stdio.h>
#include <ctype.h>
void betterguess(void)
{
char response;
int high = 100;
int low = 0;
int guess = 50;
printf("I'm going to try and guess a number from 1 to 100. If I guess your "
"number, then press y. \n");
printf("Is your number %d?\n", guess);
printf("If the number needs to be higher, then press 1.\n");
printf("If the number is lower, then press 2.\n");
while ((response = (char)tolower(getchar())) != 'y')
{
while (getchar() != '\n')
;
if (response == '1')
{ // guess = high + (high + low) / 2;
guess = guess + (high + low) / 2;
printf("Well, is that your number %d?\n", guess);
// printf("Well, is your number %d?\n", (high + (high + low) / 2 ));
}
if (response == '2')
{ // guess = low = (high + low) / 2;
guess = guess - (high + low) / 2;
printf("Well, is that your number %d?\n", guess);
//printf("Well, is your number %d?\n", (low = (high + low) / 2 ) );
}
}
printf("I knew I could do it!\n");
}
int main (void)
{
betterguess();
return 0;
}