The scanf keeps being skipped

后端 未结 3 1756
独厮守ぢ
独厮守ぢ 2020-12-22 02:32

After I formatted the \"direccion\"\'s scanf in function \"leerdatos\", it skip the \"nro_corredor\" in the second \'for\' loop.

I\'ve already read the related quest

3条回答
  •  Happy的楠姐
    2020-12-22 02:41

    One other issue you are faced with is the need to flush the input buffer (stdin) after calling scanf. As chux noted, scanf does not consume the newline '\n' leaving it in the input buffer to be read as the next character. It is always good to flush the input buffer after each use of scanf, especially when scanf is called multiple times. A simple way to flush the input buffer is:

    int c = 0;                                  /* test int for getchar */
    ...
    scanf("%d",&cp);
    do { c=getchar(); } while ( c != '\n');     /* flush input buffer */
    

    Note: fflushf does not clear characters remaining in stdin.

提交回复
热议问题