fgets() not waiting for input

后端 未结 3 1868
北海茫月
北海茫月 2020-12-11 23:49

I wrote the following code:

int N;
scanf(\"%d\", &N);
int i;
for (i = 0; i < N; i++) {
  char line[LINE_MAX];
  if (fgets(line, LINE_MAX, stdin) != NU         


        
3条回答
  •  一整个雨季
    2020-12-12 00:19

    It has nothing to do with threading. scanf() reads exactly what you ask it to; it's leaving everything else unread, notably the newline following the data. (You also aren't dealing with the possibility that the user didn't type what you intended.)

    If you want to do line oriented input, use fgets(). Don't use scanf() and hope the system can magically intuit that you want to ignore what you didn't read.

提交回复
热议问题