3

In the following piece of code what is while loop doing (marked with "loop")?:-

int main(void)
{
    char code;

    for (;;)
    {
        printf("Enter operation code: ");
        scanf(" %c", &code);
        while (getchar() != '\n')   // loop
            ;
        switch (code)
        {
        case 'i':
            insert();
            break;
        case 's':
            search();
            break;
        case 'u':
            update();
            break;
        case 'p':
            print();
            break;
        case 'q':
            return 0;
        default:
            printf("Illegal code\n");
        }
        printf("\n");
    }
}

Diclaimer:The code is not complete, it's just a part of the code because of which it won't compile.

lurker
  • 56,987
  • 9
  • 69
  • 103
chanzerre
  • 2,409
  • 5
  • 20
  • 28

6 Answers6

7

getchar() used here to eat up the extra characters entered by user and the newline character \n.
Suppose a user entered the operation code as

isupq\n // '\n' is for "Enter" button

then, scanf() would read only character i and rest of the five characters would become consumed by the statement

while (getchar() != '\n')   
        ;  

Thus for next iteration scanf() will wait for user to input a character instead of reading it from the input buffer.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • Then why compiler is skipping calls to scanf() function here? What's wrong in this code? What is the solution? See here: http://ideone.com/txlQm8 – Destructor Feb 11 '16 at 14:30
  • 1
    @PravasiMeet; In function `getdata`, on second iteration `fgets` is reading `'\n'` left over by `scanf`. Place a `getchar();` statement to eat up that newline character.http://ideone.com/Dap2Bj – haccks Feb 11 '16 at 14:55
3
while (getchar() != '\n')   // loop
;

is here to clean the buffer.

The problem that this while solves is that scanf(" %c", &code); only grabs a single character from the input buffer. This would be fine, except that there's still a newline left in the input buffer that resulted from hitting 'enter' after your input. a buffer clear is needed for the input buffer. that's what the while loop does

it is a common problem with c

No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
0

scanf() usually is not a good way to scan char variables, because the Enter you press after entering the character remains in input buffer. Next time you call scanf("%c", &input) this Enter already present in buffer gets read & assigned to input, thus skipping next input from user.

0xF1
  • 6,046
  • 2
  • 27
  • 50
0

Here application waits for the user to press enter.

Zigma
  • 529
  • 6
  • 37
0

Since the for loop in the given code is a infinite looping so the while loop is checking whether the input character is \n or not. In case the the character entered is \n it then moves toward the switch case. In general sense it is waiting of return key to be pressed to confirm your input.

Aatish Sai
  • 1,647
  • 1
  • 26
  • 41
0

if you use the fgetc function you don't have to worry and check for the enter key in your infinite loop. Even if you enter more than one character it will only take the first one

int main(void)
{
char code;

for (;;)
{
    printf("Enter operation code: ");
    code = fgetc(stdin);

    switch (code)
    {
    case 'i':
        insert();
        break;
    case 's':
        search();
        break;
    case 'u':
        update();
        break;
    case 'p':
        print();
        break;
    case 'q':
        return 0;
    default:
        printf("Illegal code\n");
    }
    printf("\n");
}
}
okaerin
  • 789
  • 5
  • 23