Thread: fflush(stdin) query

  1. #1
    Anirban Ghosh
    Join Date
    Jan 2006
    Posts
    278

    fflush(stdin) query

    I have read at few places that it is not good to use fflush (stdin) as result may be undefined.
    So what shall I use? Without this I am facing problems whenever i try to read a string from keyboard when I have read something before. I am using MinGW compiler.

  2. #2
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Well, here is the explanation of why it is wrong(which I believe you know of already) -

    Cprogramming.com FAQ > Why fflush(stdin) is wrong

    and here is the solution -

    Cprogramming.com FAQ > Flush the input buffer

  3. #3
    Anirban Ghosh
    Join Date
    Jan 2006
    Posts
    278
    Thanks a billion..

  4. #4
    Anirban Ghosh
    Join Date
    Jan 2006
    Posts
    278
    But what does the following line signify as given in the link u have given..

    Code:
    while ((ch = getchar()) != '\n' && ch != EOF);

  5. #5
    Registered User Cooloorful's Avatar
    Join Date
    Feb 2009
    Posts
    59
    It just one by one pulls out any letters queued up in the input buffer until an EOL or EOF comes up.

  6. #6
    Anirban Ghosh
    Join Date
    Jan 2006
    Posts
    278
    Pardon.
    EOL OR EOF as u have said. But in the statement it is written EOL AND EOF.
    So please explain..

  7. #7
    Registered User Cooloorful's Avatar
    Join Date
    Feb 2009
    Posts
    59
    I didn't mean logically "OR" I mean semantically or. If an EOL comes up, the loop ends. If an EOF comes up, the loop ends.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    More specifically, the opposite of or is and. You want to stop when you get EOL or EOF -- but you have to write the loop to say when you want to keep going.

  9. #9
    Registered User
    Join Date
    Jul 2009
    Posts
    40
    this thread can help you as well.

  10. #10
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    while ((ch = getchar()) != '\n' && ch != EOF);

    Pardon.
    EOL OR EOF as u have said. But in the statement it is written EOL AND EOF.
    So please explain..
    Whenever a single item in an expression containing the logical && operator evaluates to false, no other comparisons are made and the result is false.

    So, if either EOL or EOF is reached, the statement will evaluate to false and stop further evaluation.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Loop while the fetched character is not '\n' AND the fetched character is not EOF.
    So if the fetched character == '\n', what happens? The first condition is false, the second condition is true, but since it's AND, it must be false, right? So the loop breaks.
    And if the fetched character == EOF, what happens? The first condition is true, the second condition is false, and since it's AND, it must be false again, right? So the loop breaks.

    So we see that if either of the conditions (the left or right) in the loop becomes false, the loop breaks.
    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.

  12. #12
    Registered User
    Join Date
    Jul 2009
    Posts
    40
    You can google out "Truth Table" it'll help you understand it easily.

  13. #13
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    They mean "if c is not \n and c is not EOF". The same as "if c is neither \n nor EOF"

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. query problem
    By arian in forum C# Programming
    Replies: 1
    Last Post: 08-18-2008, 01:49 PM
  3. fread/fwrite
    By chopficaro in forum C Programming
    Replies: 6
    Last Post: 05-11-2008, 01:48 AM
  4. Searching Into Files
    By St0rM-MaN in forum C Programming
    Replies: 12
    Last Post: 04-26-2007, 09:02 AM
  5. Linked List Need Help Urgent
    By ykchua in forum C Programming
    Replies: 5
    Last Post: 08-17-2004, 02:57 PM