flush stdin

This is a discussion on flush stdin within the C Programming forums, part of the General Programming Boards category; Hey, now i know your not sposed to flush(stdin) nstuff, but can someone give me an alternative please. I have ...

  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
    22,113
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,185
    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
    Posts
    22,897
    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, 02:01 AM
  2. Replies: 2
    Last Post: 07-12-2008, 01:00 PM
  3. value of stdin (not zero?)
    By taisao in forum Linux Programming
    Replies: 3
    Last Post: 05-27-2007, 09: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, 08:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21