Thread: flush stdin

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    53

    flush stdin

    Hey, now i know your not sposed to flush(stdin) nstuff, but can someone give me an alternative please. I have used fscanf to read many doubles to a dynamic memory allocated pointer. I have then called the function in main and now the program is complete. all i want is for the program to pause before it exits so I chucked in a getchar(); at the end but obviously it skips it because of the return carriage in stdin or whatever happens there.

    Is there something simple to do to get rid of this problem

    Thanx guys

  2. #2
    Registered User
    Join Date
    Mar 2008
    Posts
    53
    any1???

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    13
    Does using getch(); work any better?

    Also, can someone illustrate the reasons why fflush(stdin) should not be used?

    I'm not an expert or anything but I don't see what's wrong with it. When I use fgets or if I have characters like newlines and stuff in the input stream fflush(stdin) almost always solves the problem.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The main reason not to use it is that it doesn't exist. fflush only works on output streams (or at least that's what the standard says; your compiler may decide to not cause an error, but that's neither here nor there).

    The same thing is true of getch(); it does not exist in standard C.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by george_1988 View Post
    fflush(stdin) almost always solves the problem.
    I think you just answered your own question.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The standard does not say that fflush(stdin) is defined, and therefore it is undefined. It may work, it may not work. And for some time you may find that it works, but then suddenly you find that it doesn't work. Because it's undefined.
    The story on getch is that it is non-standard (it's an extension not supported by all compilers). Instead, you should use getchar() or get a better IDE that allows you to run the app w/o closing it or get a debugger that allows you to put a breakpoint at the end of main so the window doesn't close or run the darn thing from the command line.
    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.

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    The story on getch is that it is non-standard (it's an extension not supported by all compilers). Instead, you should use getchar() or get a better IDE that allows you to run the app w/o closing it or get a debugger that allows you to put a breakpoint at the end of main so the window doesn't close or run the darn thing from the command line.
    That's not the only reason people use getch() or getchar() though. Actually, I've never used it at the end of my programs, since I always use VC++. I like getch() or getche() because I can create a simple interactive console-based menu without having to press enter after a key.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

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. Replies: 2
    Last Post: 07-12-2008, 12:00 PM
  3. value of stdin (not zero?)
    By taisao in forum Linux Programming
    Replies: 3
    Last Post: 05-27-2007, 08:14 AM
  4. stdin question
    By oomen3 in forum C Programming
    Replies: 6
    Last Post: 02-19-2003, 02:52 PM
  5. How do I print a pattern flush right?
    By Basia in forum C Programming
    Replies: 5
    Last Post: 06-11-2002, 07:15 AM