fgets() not waiting for input

后端 未结 3 1872
北海茫月
北海茫月 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:29

    You can place a call to fflush() just after the scanf() like this:

    int N;
    scanf("%d", &N);
    fflush (stdin);
    int i;
    ... (rest of code)...
    

    So the newline character ges erased from the stdin buffer and the next fgets() will stop to ask for input.

提交回复
热议问题