Thread: why fflush(stdin) wrong?

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    183

    why fflush(stdin) wrong?

    i wanted to ask why fflush(stdin) wrong
    i saw faq but i couldnt rlly understand its not so wrong...
    that it flushes stream ...
    sometimes scanf gives \n how can i bypass this problem without fflush(stdin).

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    This has been covered 1000's of times. You either use something that eats up characters until the buffer is empty (it's in the FAQ- you should have seen it) or if you're pro, you read the entire line and parse it yourself.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  3. #3
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    It gives you undefined behavior which makes your program work at sometime and not work at other times. that's why.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Because it's

    1. Not compliant with the standards
    2a. Non portable if it does work
    2b. Undefined if it does "work"

    The FAQ explains well why you shouldn't do it, it also contains an alternative.

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    oh ok thanks

  6. #6
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    could i also use fgets to read in integer?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by lolguy
    could i also use fgets to read in integer?
    You can use it to read in a string, whether the string is numeric or not. I am not sure what exactly that has to do with "flushing" the input buffer since using getchar() in a loop is usually enough.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    I talked to a friend who's studying in an institute. I saw that the use of scanf gets and fflush are being taught to them. The book about C that he gave to me is also teaching about that. I have been using that for 8 months of studying C and C++.. and now it's not nice to use it.. I wonder why publisher would sell those kind of books that teach wrong.. or maybe they just wanted to let the reader know that those functions exist and let the reader discover that it's not good to use.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Although gets is beyond redemption, there is nothing wrong about using scanf and fflush. For example, fflush on an output stream is well defined.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I take it it is more along the lines of using scanf to read an integer, then flushing the input stream.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I wonder why publisher would sell those kind of books that teach wrong
    A technical review is rather expensive, and a lot of the time if a book looks good to the layman publisher then it'll get published regardless of the technical quality. The sad reality is that most of the authors that write a programming book are not experts on the topic. Herbert Schildt comes to mind. He's a great writer, but not the master programmer his books shamelessly suggest.

    A book doesn't have to be perfect to be useful, as long as you don't take what it says as gospel.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  2. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  3. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  4. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM
  5. What is wrong with my while loop?
    By aspand in forum C Programming
    Replies: 3
    Last Post: 06-19-2002, 12:07 PM