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

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

  2. junk mail <> scribbled the following:
    > 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.


    Can't be done in ISO standard C.

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


    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 Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "No, Maggie, not Aztec, Olmec! Ol-mec!"
    - Lisa Simpson
     
    Joona I Palaste, Feb 28, 2004
    #2
    1. Advertising

  3. junk mail

    Mike Wahler Guest

    "Joona I Palaste" <> wrote in message
    news:c1r2gd$6o$...
    > junk mail <> scribbled the following:
    > > 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.

    >
    > Can't be done in ISO standard C.


    #include <stdio.h>

    int main()
    {
    int count = 0;

    do
    {
    int c = 0;
    count = 0;
    printf("Press ENTER to continue...");
    fflush(stdout);

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

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

    } while(count);

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

    :)


    -Mike
     
    Mike Wahler, Feb 28, 2004
    #3
  4. Mike Wahler <> scribbled the following:
    > "Joona I Palaste" <> wrote in message
    > news:c1r2gd$6o$...
    >> junk mail <> scribbled the following:
    >> > 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.

    >>
    >> Can't be done in ISO standard C.


    > #include <stdio.h>


    > int main()
    > {
    > int count = 0;


    > do
    > {
    > int c = 0;
    > count = 0;
    > printf("Press ENTER to continue...");
    > fflush(stdout);


    > while((c = getchar()) != '\n' && c != EOF)
    > ++count;


    > if(count)
    > puts("ENTER only, I say!");


    > } while(count);


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


    > :)


    Which doesn't solve the problem of characters getting echoed to the
    screen, so the code is useless.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "Shh! The maestro is decomposing!"
    - Gary Larson
     
    Joona I Palaste, Feb 29, 2004
    #4
  5. junk mail

    Mike Wahler Guest

    "Joona I Palaste" <> wrote in message
    news:c1s3np$gah$...
    > Mike Wahler <> scribbled the following:
    > > "Joona I Palaste" <> wrote in message
    > > news:c1r2gd$6o$...
    > >> junk mail <> scribbled the following:
    > >> > 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.
    > >>
    > >> Can't be done in ISO standard C.

    >
    > > #include <stdio.h>

    >
    > > int main()
    > > {
    > > int count = 0;

    >
    > > do
    > > {
    > > int c = 0;
    > > count = 0;
    > > printf("Press ENTER to continue...");
    > > fflush(stdout);

    >
    > > while((c = getchar()) != '\n' && c != EOF)
    > > ++count;

    >
    > > if(count)
    > > puts("ENTER only, I say!");

    >
    > > } while(count);

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

    >
    > > :)

    >
    > Which doesn't solve the problem of characters getting echoed to the
    > screen, so the code is useless.


    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
     
    Mike Wahler, Feb 29, 2004
    #5
  6. junk mail

    Dan Pop Guest

    In <sU70c.929$GQ.883@newsfe1-win> "junk mail" <> writes:

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


    He's lucky, because Enter/Return is the only key for which a standard
    solution exists.

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


    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.

    >For example:-
    >
    >do
    > { /* Begin loop */
    > printf("\Press Enter"); /* Output to screen */
    > } while ((ch = getchar()) != '\n') ; /* While ( Condition -
    >must press enter ) */


    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
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Mar 1, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew Robert
    Replies:
    51
    Views:
    22,710
    Mark McIntyre
    Nov 13, 2003
  2. Timur Ametov

    Message "Press any key to continue"

    Timur Ametov, Apr 26, 2006, in forum: C++
    Replies:
    8
    Views:
    467
    Default User
    Apr 27, 2006
  3. Susan Rice
    Replies:
    9
    Views:
    1,003
    Marcus Kwok
    Jul 11, 2006
  4. Karthik
    Replies:
    15
    Views:
    956
    Joachim Schmitz
    Jun 24, 2008
  5. HopfZ
    Replies:
    0
    Views:
    597
    HopfZ
    Aug 28, 2006
Loading...

Share This Page