Thread: How do I get my program to wait for a keypress?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    15

    How do I get my program to wait for a keypress?

    Code:
    #include <stdio.h>
    
    void myflush ( FILE *in )
    {
      int ch;
    
      do
        ch = fgetc ( in ); 
      while ( ch != EOF && ch != '\n' ); 
    
      clearerr ( in );
    }
    
    void mypause ( void ) 
    { 
      printf ( "Press [Enter] to continue . . ." );
      fflush ( stdout );
      getchar();
    } 
    
    int main ( void )
    {
      int number;
    
      // Test with an empty stream
      printf ( "Hello, world!\n" );
      mypause();
    
      // Leave extra input in the stream
      printf ( "Enter more than one character" );
    
      myflush ( stdin );
      mypause();
    
      return 0;
    }
    What is the function of "fflush ( stdout );" and "clearerr ( in );" in this code?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Have you looked in the manual pages for those functions?
    What did it say?
    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.

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    fflush(stdout); flushes the output stream (stdout) to ensure no chars are in the stream that could echo, 'myflush' is used to eat all chars in the input stream that could potentially automatically trigger getchar(); to return prematurely.

    And clearerr(stream); resets all the errors/EOFs set for that specific stream, see http://www.cplusplus.com/reference/c.../clearerr.html
    Last edited by zacs7; 05-07-2007 at 03:48 PM.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    15
    Yes, I looked in the manual pages for those functions, and I always do a research before posting a thread.
    http://www.space.unibe.ch/comp_doc/c...MAN/fflush.htm
    the question I should be asking is why we do use "fflush ( stdout );" and "clearerr ( in );" here?
    because I get the same result without this 2 lines.
    Obviously we use it to avoid something which I want to know about.
    Last edited by redche; 05-07-2007 at 06:47 AM.

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    ffs, read my post.

  6. #6
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    you NEVER EVER flush st.... oh

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    um, yes you can flush stdout

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. making my program sleep / wait / delay...
    By bobthebullet990 in forum C++ Programming
    Replies: 4
    Last Post: 08-13-2006, 10:14 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  4. How to make a program wait for an event?
    By DRoss in forum C++ Programming
    Replies: 0
    Last Post: 09-10-2001, 01:13 PM
  5. How can I make a program wait for an event?
    By DRoss in forum C++ Programming
    Replies: 0
    Last Post: 09-06-2001, 09:28 AM