The program is suppose to guess the value a user is thinking, the program askes if the number it guessed is higher or lower than the users value. Once the program find the correct number, the program is suppose to display " i knew i could to it" and once the user hits enter, it terminates. I can get the program to display the text. But, after i hit enter it backs in to the loop instead of terminating.
Can any one help? Thanks,
Code:
#include <stdio.h>
int main(void)
{
int hi, lo, guess;
char answer;
hi = 100;
lo = 0;
guess = (hi - lo) / 2;
printf( "Pick a number and I will try to guess your number\n\n");
do
{
printf ("%d:", guess);
printf (" is this number lower(l), higher(h), or correct(c)");
switch (getchar())
{
case 'h':
hi = guess;
guess = lo + (hi - lo)/2;
break;
case 'l':
lo = guess;
guess = lo + (hi - lo)/2;
break;
case 'c':
printf(" I knew I could do it");
getchar();
}
}
while ( getchar()!= 'c');
return 0;
}