0
votes

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;
}
2
choice never changes in the while loop and the break in switch only breaks switch, not the while loop. - Shiping
you may replace getchar() after the question with scanf("%c" , &choice); - Shiping
Thank-you. I am still trying to learn c programming. - AncientOwl
One of the points of coursework is to introduce you to the notion of debugging. Have you tried anything? - user1531971
Welcome to Stack Overflow! It looks like you need to learn to use a debugger. Please help yourself to some complementary debugging techniques. If you still have issues afterwards, please feel free to come back with more details. - Joe C

2 Answers

0
votes

Well, I modified some of your code so it does what you want. I recommend you to stop using variables names such as Integer. Also, you should name every variable lowercase, so you maintain a structure.

Hope this code helps you solving some issues and getting started with C coding!

#include <stdio.h>
#include <stdlib.h>

int Guess(int upperBound, int lowerBound){
    return (lowerBound+upperBound) / 2; // computer's guess is the midpoint 
}

int main(void){

//variables
int number = 0;
int lowerBound = 1;
int upperBound = 0;
int guess = 0;
char choice;
int guessCount = 0;


//enter a number n
printf("Enter n: ");
scanf("%d" , &number);

getchar();

upperBound = rand()%(number*10);
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);
getchar();

while (choice != 'e'){
    if(choice == 'l') {
        lowerBound = guess + 1;         
    } else if (choice == 's') {
        upperBound = guess - 1;            
    }
    guess = Guess(upperBound , lowerBound);
    guessCount++;
    printf("Is this your number %d? " , guess);
    scanf("%c" , &choice);
    getchar();
}
//if user enters 'e' then the computer guess right, prints the x value, number of guesses and program ends
printf("Your number must be %d. I used %d guesses." , guess , guessCount);

return 0;
}

I also encourage you to find better and more accurate alternatives to solve this same problem! Good luck.

0
votes

It seems that you are beginner to c programming. No need to use getchar after scanf.

for your problem, you are taking user input only once, note: its outside the while loop. Hence you are not able to modify it inside while, and getting into infinite loop. Also, your last part, where you are checking for choice = e is also going out of while loop scope.

for this problem, better u can use do while loop as below:

 do {
        printf("Is your number %d? " , Guess );
        scanf("%c" , &choice);
        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);
        }
      } while (choice == 'l' || choice == 's');