fflush(stdin) function does not work

后端 未结 3 677
甜味超标
甜味超标 2020-11-28 16:28

I can\'t seem to figure out what\'s wrong with this code:

#include 
#include 
#include 
#include 

        
相关标签:
3条回答
  • 2020-11-28 17:03

    The fflush function does not flush data out of an input stream; it is instead used to push data buffered in an output stream to the destination. This is documented here. As seen in this earlier SO question, trying to use fflush(stdin) leads to undefined behavior, so it's best to avoid it.

    If you want to eat the newline from the return character entered when the user finished typing in their character, instead consider the following:

    scanf("%c%*c", &sect_cat);
    

    This will eat the newline rather than leaving it in stdin.

    Hope this helps!

    0 讨论(0)
  • 2020-11-28 17:13

    fflush should work with an output stream, see docs here

    0 讨论(0)
  • 2020-11-28 17:19

    I think that you meant to write fflush(stdout) instead of fflush(stdin).

    0 讨论(0)
提交回复
热议问题