Press enter to continue

Discussion in 'C Programming' started by junk mail, Feb 28, 2004.

  1. junk mail

    junk mail Guest

    My friend is trying to code a small c program where he wants to force the
    user to press enter and only enter to continue.

    Currently he is using getchar() with a loop but you can type any number of
    characters, which are echoed to screen before you have to press enter.

    For example:-

    { /* Begin loop */
    printf("\Press Enter"); /* Output to screen */
    } while ((ch = getchar()) != '\n') ; /* While ( Condition -
    must press enter ) */
    junk mail, Feb 28, 2004
    1. Advertisements

  2. Can't be done in ISO standard C.
    ISO standard C can only read from stdin and write to stdout. It cannot
    control anything about what happens to characters before they come to
    stdin, or after they go to stdout.
    Doing what your friend wants requires meddling with the characters
    before the "read from keyboard" and "put into stdin" phases. This is
    outside of C's scope and requires OS-specific code, which we don't
    discuss here.
    Joona I Palaste, Feb 28, 2004
    1. Advertisements

  3. junk mail

    Mike Wahler Guest

    #include <stdio.h>

    int main()
    int count = 0;

    int c = 0;
    count = 0;
    printf("Press ENTER to continue...");

    while((c = getchar()) != '\n' && c != EOF)

    puts("ENTER only, I say!");

    } while(count);

    puts("Thank you.");
    return 0;


    Mike Wahler, Feb 28, 2004
  4. Which doesn't solve the problem of characters getting echoed to the
    screen, so the code is useless.
    Joona I Palaste, Feb 29, 2004
  5. junk mail

    Mike Wahler Guest

    I wasn't sure if inhibiting 'echo' was a strict requirement.
    The code above does eliminate the problem of the 'extra'
    characters getting submitted to subsequent inputs, which
    is indeed a common complaint. But yes, you're right, no
    way to inhibit the echo with standard code.

    Mike Wahler, Feb 29, 2004
  6. junk mail

    Dan Pop Guest

    He's lucky, because Enter/Return is the only key for which a standard
    solution exists.
    So what? The program is still guaranteed to wait until Enter is pressed.
    There is no way to prevent the user from pressing anything he wants
    (short of giving him a keyboard with a single key), but the program can
    still wait until the Enter key is pressed and discard everything, the
    Enter key included.
    In such cases you may also want to detect the user typing the eof key,
    and handle this case separately. Otherwise, you may end up waiting for
    an Enter key that will *never* come.

    int rc = scanf("%*[^\n]");
    if (rc < 0) /* the eof key was pressed */ ;
    else getchar(); /* remove the newline character from stdin */

    Dan Pop, Mar 1, 2004
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.