Question
I am trying to get the computer to guess my secret number using low, high or correct (l,h, or c) input. The program should start
I am trying to get the computer to guess my secret number using low, high or correct (l,h, or c) input. The program should start at 50 and then split the difference if wrong. Example; Is is 50? (l) Is it 75? (h) Is it 63? (c) I knew I could do it!
Right now the program spits out the error "Sorry, I understand only l, h or c." before it gets any input and then when the user picks (l) the program does not recognize it the first time. I am guessing it is a buffer thing but I can not figure out how to fix it, can you?
#include
void betterguess(void) { int guess = 50; char response;
printf("Think of an integer from 1 to 100. I will try to guess "); printf("it. Respond with an l if my guess is low and with"); printf(" an h if it is too high or a c if it is correct. "); printf("Uh...is your number 50? "); while ((response = getchar()) != 'c') /* get response */ { if (response == 'l') printf("Well, then, is it %d? ", guess + (guess/2)); else{ printf("Sorry, I understand only l, h or c. "); } while (getchar() != ' ') continue; /* skip rest of input line */ } printf("I knew I could do it! "); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started