Is Hitting Enter to Continue in C a Char

  1. #1

    rox is offline

    Registered User


    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

    Thantos is offline

    & the hat of GPL slaying Thantos's Avatar


    EOF is used for file manipulation, rarely for use with standard input. Try using getchar() and comparing that to \n


  3. #3

    Prelude is offline

    Code Goddess Prelude's Avatar


    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

    rox is offline

    Registered User


    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

    ronin is offline

    Casual Visitor


    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

    Thantos is offline

    & the hat of GPL slaying Thantos's Avatar


    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

    somewhere.

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


  7. #7

    rox is offline

    Registered User


    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

    ronin is offline

    Casual Visitor


    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

    Hammer is offline

    End Of Line Hammer's Avatar


    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]


  10. #10

    linuxdude is offline

    Registered User linuxdude's Avatar


    fflush(stdout);

    quoteI 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

    Salem is offline

    and the hat of int overfl Salem's Avatar


    > 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.


  12. #12

    Prelude is offline

    Code Goddess Prelude's Avatar


    >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

    WaltP is offline

    Been here, done that.


    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

    Prelude is offline

    Code Goddess Prelude's Avatar


    >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

    WaltP is offline

    Been here, done that.


    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


petersonmonely.blogspot.com

Source: https://cboard.cprogramming.com/c-programming/47799-press-enter-continue-beginner-code.html

0 Response to "Is Hitting Enter to Continue in C a Char"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel