Thread: Press enter to continue... beginner code

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    34

    Press enter to continue... beginner code

    i'm doing a printf, than i want to stop and ask the user to press enter to continue (that would return him/her to a main menu.... now i got it to stop and wait for input, but when pressing enter it only gives me a new line... here's the code:

    Code:
    void pause(void){
    	printf("Press ENTER to continue.... \n");
    	while(getc() != EOF) /* nothing happens */;
    }
    what am i doing wrong here?

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    EOF is used for file manipulation, rarely for use with standard input. Try using getchar() and comparing that to \n

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    EOF and '\n' are not the same. Try something more like this:
    Code:
    void pause ( void )
    {
      printf ( "Press enter to continue..." );
      fflush ( stdout );
      getchar();
    }
    However, if there are any leftover newlines in the stream, this won't pause. It's up to you to keep your streams clean. Save the the environment!
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    34
    Great it works like so:

    Code:
            printf("Press enter to continue: ");
            while(getchar() != '\n');
    Now i'm stumped with a parse error on a line next after the last line in my program. That's very helpfull.

    more reading for me.

    Thanks guys!

  5. #5
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    probably the semi on the while... sort of like if(something);

    try
    Code:
    while(getchar() != '\n'){}
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    probably the semi on the while... sort of like if(something);
    ronin he is trying to create a pause so the ; is intentional.

    A parse error at the end of the file would seem to indicate a extra or missing
    Code:
    }
    somewhere.

    Note: the javascript that forces you to put the bracket in code tags is really annoying.

  7. #7
    Registered User
    Join Date
    Nov 2003
    Posts
    34
    yeah, i found that one too... in another function... it's so weird whe it does not give you any clue as to where to look for the error

  8. #8
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Originally posted by rox
    yeah, i found that one too... in another function... it's so weird whe it does not give you any clue as to where to look for the error
    That's what I was getting at. What one expects to be the cause isn't always the case. One little flub can escalate into a major catastrophe.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926

    fflush(stdout);

    quote
    Code:
    fflush ( stdout );
    I was wondering why you can do this for stdout but not stdin I have read the faqs and it is a little too technical for me. Maybe because I don't understand what a stream is really well. Could someone could give me a concrete down to earth answer? Thanx in advance

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > I was wondering why you can do this for stdout but not stdin
    The standard says fflush() is applicable to
    - output streams (stdout is an output stream)
    - update streams where the last operation was a write.

    Since stdin is never either of those cases, you can never use fflush on stdin

    On stdout, it means that whatever characters have been written so far will be pushed out of the buffering in the standard library and onto the operating system.
    stdout is flushed automatically if
    - you print a newline character
    - the internal buffer becomes full.

    The typical use is making sure a prompt appears on the stdout, when that prompt doesn't contain a newline
    printf( "Enter name > " );
    fflush( stdout );

    In some environments, if you omit the fflush(), you may not see the prompt at the moment you expect to see it.
    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.

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I was wondering why you can do this for stdout but not stdin
    Because requiring a single function to perform two completely different actions is illogical. Think about it: flushing an output stream means to send buffered data to its final location. Flushing an input stream means to discard the remaining buffered data. These are inherently incompatible operations, so using fflush for both makes little sense.

    Of course, the simpler "why" is that the standard says so, and smart programmers follow the standard.
    My best code is written with the delete key.

  13. #13
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by Salem
    Since stdin is never either of those cases, you can never use fflush on stdin
    Don't say "never" Salem. As you well know a few compilers have extended the standard so that fflush(stdin); functions as expected. That doesn't make it standard, but it does make it as acceptable as getch() when defined.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >As you well know a few compilers have extended the standard
    Unless we say otherwise, we're talking about standard C when we talk about things you can or cannot do and things that are or are not possible.

    >Don't say "never" Salem.
    This is true, the standard committee may decide to add that functionality. It isn't likely, but not outside the bounds of reason.
    My best code is written with the delete key.

  15. #15
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    So you're saying,
    Never use getch().
    Never use graphics packages.
    Never use conio.h nor ncurses.h.
    Never use anything unless it conforms 100% to the standard (which one? C89, C99?)

    Come on, you know better.

    In some cases to accomplish a specific task and you have the compiler available, you must use extensions. And this board should help even with those extensions. But, where possible, describe the "way of the standard."

    I believe the statement "Since stdin is never either of those cases, you can never use fflush on stdin" should be modified to "Since stdin is never either of those cases, you should not rely on fflush on stdin, it does not conform to the standards."

    Just my 2¢
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Capture Enter key press in EDIT Box
    By richiev in forum Windows Programming
    Replies: 4
    Last Post: 07-14-2005, 12:03 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. press enter to continue
    By Klinerr1 in forum C++ Programming
    Replies: 12
    Last Post: 06-27-2002, 11:43 AM
  4. Press enter to continue
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 03-29-2002, 11:18 PM
  5. terminate 0 - PLEASE HELP
    By Unregistered in forum C Programming
    Replies: 11
    Last Post: 11-21-2001, 07:30 AM