4

I'm trying to make a loop in C where the program calculates the avarage of 2 numbers and then waits for user input. If the user input is 'G' then the loop will break. However this is not working currently because it's (in a strange way) a infite loop.

My code is:

while(1){
    pogingen++;
    gem = (minimum+maximum)/2;
    printf("%i",gem);
    scanf("%c",&invoer);


    if(invoer=='L'){
        maximum = gem;
    }
    if(invoer=='H'){
        minimum = gem;
    }
    if(invoer=='G'){
        printf("%i",pogingen);
        break;
    }
}

I tested it with these values: minimum = 1, maximum = 10. The result will be an infite loop of 5's. It doesn't even wait for the user input (which it's supposed to do.)

Thanks in advance for looking at this!

1
  • In your case, add a space before the %c, it would work: scanf(" %c",&invoer); Commented Apr 1, 2014 at 10:22

4 Answers 4

4

It doesn't even wait for the user input (which it's supposed to do.).
The program is not waiting to get the input means there is some character left in input buffer. possibly \n from previous input. So clear the input buffer before reading input.
you can put,

getchar();
scanf("%c",&invoer);

before scanf() inside loop;

Sign up to request clarification or add additional context in comments.

4 Comments

Unconditionally adding a getchar above the scanf might not be a good idea. What if this is the first iteration in the loop? What if the user enters multiple input-characters at once? If anything, put it after the scanf and then make sure that you discard only spaces.
@JoachimPileborg But there is a scanf() executed everytime inside loop and it is likely that the input is given ending by <kbd>enter<kbd> since there is only 1 element to read. in this case particular program so getchar is fine. this is what i think. anyway thankyou
Think again what will happen the very first iteration of the loop, the first time the user is asked to enter input, when nothing else have been read from the user. What will your getchar call read then?
@JoachimPileborg. if there is a scanf() before entering loop then this will not be a problem. and the code may read 2 numbers before entering loop.
3

The reason it doesn't wait for user input in some instances, is because when scanf reads a character, you press the Enter key at the end of the of input, and that key is also stored in the input buffer. So the next iteration it will read that enter key.

This is easily solved by telling scanf to discard trailing whitespace (which the newline character is):

scanf("%c ",&invoer);
/*       ^                 */
/*       |                 */
/* Notice extra space here */

You might also want to print some error message if the user doesn't give valid input. Also, consider using toupper, because the chances are the user will not give you an upper-case letter.

It might also be better to to use e.g. if ... else if ... else ... instead. Or possibly use a switch statement.

1 Comment

This didn't work in my case, but the other answer with getchar() did work, thanks though!
0

simple and sweet solution invoer=getchar(); that will wait for a character as well as store in to the variable. no need to write scanf

Comments

0
invoer=getchar(); 

is a solution if you flush stdin before using it for the next iteration like so:

fflush(stdin);
invoer=getchar();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.