Why is scanf() causing infinite loop in this code?

后端 未结 16 1492
日久生厌
日久生厌 2020-11-21 06:20

I\'ve a small C-program which just reads numbers from stdin, one at each loop cycle. If the user inputs some NaN, an error should be printed to the console and the input pro

16条回答
  •  無奈伤痛
    2020-11-21 07:18

    I think you just have to flush the buffer before you continue with the loop. Something like that would probably do the job, although I can't test what I am writing from here:

    int c;
    while((c = getchar()) != '\n' && c != EOF);
    

提交回复
热议问题