Thread: fflush(stdin);?!?!

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    4

    fflush(stdin);?!?!

    Can anyone help tell me what this does. I came across this in a program and I am unable to understand what it does. Would this be of any help in my programs, or can I live without it. Any help would be greatly appreciated...Thanks in advance.

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Next time maybe do a board and google search before asking.

    Hope this help.

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    4
    I've actually found the same site, and it tells me what it does. but i haven't found out why i need it. everytime i take it out of the program it gets stuck in a loop. Do i only need it if i'm going to scanf something after i've already scanf'd an arrary. once again, any help would be appreciated and thanks in advance for any you can give.

  4. #4
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    let's say that you asked the user to input a char, using getchar(), for some reason, the user enter more than one char, later, when you try to read another char, getchar() will return the extra char the user entered, to avoid this, you can use fflush(stdin), after reading the first char, and the second one will disappear.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    And why you should not use fflush(stdin) is in the FAQ
    As are a bunch of ideas on better ways to read input which don't involve the messyness of scanf()

    But since you're using C++, perhaps you should consider the C++ equivalents.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >But since you're using C++, perhaps you should consider the C++ equivalents.
    cin's >> operator has the same issues as scanf, but C++ is different in that it has a standard solution equivalent to fflush on stdin:
    Code:
    cin.ignore ( numeric_limits<streamsize>::max(), '\n' );
    Of course, this doesn't make it any cleaner (it simply isn't undefined behavior).
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. fread/fwrite
    By chopficaro in forum C Programming
    Replies: 6
    Last Post: 05-11-2008, 01:48 AM
  3. Searching Into Files
    By St0rM-MaN in forum C Programming
    Replies: 12
    Last Post: 04-26-2007, 09:02 AM
  4. Newbie Question - fflush(stdin) & fpurge(stdin) on Mac and PC
    By tvsinesperanto in forum C Programming
    Replies: 34
    Last Post: 03-11-2006, 12:13 PM
  5. Linked List Need Help Urgent
    By ykchua in forum C Programming
    Replies: 5
    Last Post: 08-17-2004, 02:57 PM