I am having issues with a while loop in my c program for an introductory course in c programming. I need to tell the computer if its number guess is larger or smaller then when it is correct it should print the number and number of guesses. After the program enters the while loop it will not exit and I am unsure how to fix it.
Sample of what the program needs to do
Enter n: 50
Is your number 25? l
Is your number 38? l
Is your number 44? s
Is your number 41? e
Your number must be 41. I used 4 guesses.
Here is my current code:
#include <stdio.h>
int Guess(int upperBound, int lowerBound){
int Guess = 0;
Guess = (lowerBound+upperBound) / 2; // computer's guess is the midpoint
return Guess;
}
int main(void){
//variables
int Integer = 0;
int lowerBound = 1;
int upperBound = 0;
int Guess = 0;
char choice;
int guessCount = 0;
//enter an integer n
printf("Enter n: ");
scanf("%d" , &Integer);
getchar();
upperBound = Integer;
Guess = Guess(upperBound , lowerBound); //send to function to get computer's guess
// computer prints its choice 'x' and asks if it is larger or smaller than n.
printf("Is your number %d? " , Guess );
scanf("%c" , &choice);
while (choice == 'l' || choice == 's'){
switch (choice){
case 'l':
lowerBound = Guess + 1;
++guessCount;
Guess = numGuess(upperBound , Guess);
getchar();
printf("Is this your number %d? " , Guess);
getchar();
break;
case 's':
upperBound = Guess - 1;
++guessCount;
Guess = numGuess(upperBound , lowerBound);
getchar();
printf("Is this your number %d? " , Guess);
getchar();
break;
default:
break;
}
}
//if user enters 'e' then the computer guess right, prints the x value, number of guesses and program ends
if (choice == 'e'){
printf("Your number must be %d. I used %d guesses." , Guess , guessCount);
}
return 0;
}