Alternate method for clearing input buffer in c

后端 未结 3 1625
忘掉有多难
忘掉有多难 2021-01-14 23:50

Is there any other method to clear the input buffer in c withut using

  fflush();

or

  while(getchar()!=\'\\n\');
<         


        
3条回答
  •  死守一世寂寞
    2021-01-15 00:26

    The best solution is to not depend on the input buffer's state so much.

    Read input as whole lines, using fgets(), then parse those. Don't use e.g. scanf() to read individual values, since it interacts with the buffer in annoying ways.

提交回复
热议问题