Press any key

Discussion in 'C++' started by John Howard, Jul 12, 2003.

  1. John Howard

    John Howard Guest

    I'm trying to write a simple press any key routine, but am having some
    problems. Here is the routine:

    void PressAnyKey ()
    {
    cout << "Press Any Key";
    do{}while (!kbhit());
    getch();
    cout << endl;
    }

    When it runs, the next part of the code is to read using a similar kbhit(),
    getch() routine, but it reads in whatever was pressed during the press a
    key. If I add an extra getch(), then it just sits there until you press it
    twice.

    Any ideas?

    John
     
    John Howard, Jul 12, 2003
    #1
    1. Advertising

  2. John Howard

    MiniDisc_2k2 Guest

    "John Howard" <> wrote in message
    news:IPUPa.51517$...
    > I'm trying to write a simple press any key routine, but am having some
    > problems. Here is the routine:
    >
    > void PressAnyKey ()
    > {
    > cout << "Press Any Key";
    > do{}while (!kbhit());
    > getch();
    > cout << endl;
    > }
    >
    > When it runs, the next part of the code is to read using a similar

    kbhit(),
    > getch() routine, but it reads in whatever was pressed during the press a
    > key. If I add an extra getch(), then it just sits there until you press

    it
    > twice.
    >
    > Any ideas?
    >
    > John
    >
    >


    I'm not sure if I'm getting your question right, but it seems like the
    program is running so fast that the next "press any key" loop is called
    while the user is still holding down the button? You could just add in a
    delay, something like

    for (unsigned long x = 100000; x!=0; x--);

    I doubt that's long enough, but just keep increasing the number. Of course,
    this is a bad way to do it, as CPU speeds change, and thus that loop will
    not be the same length of delay on every computer. I, unfortunately, cannot
    see another standard way of doing this.

    Most compilers, however, have an extention, the system() function, which is
    nonstandard, but can do exactly what you want. It literally tells the OS to
    do something, so by its very nature it is very OS dependent. Do this only if
    you are going to use this on a single OS. For example, in Win32 (and perhaps
    even Win16, but nobody cares about that anymore) you could do:

    system("pause");

    In Win32, system() is the equivalent of going start->run and typing in
    whatever's inside those quotes. Pause is a recognized DOS statement which
    displays "Press any key to continue..." and waits for a keypress. That
    should do better if you're willing to do something nonstandard.

    Another non-standard option but commonly found extention is also available
    for your compiler. Check for something like wait() or something like that.
    This function will usually allow you to pause for an integral number of
    seconds. This is, obviously, a better way of doing it than the delay loop,
    as it will remain the same speed on every computer.

    Hope that helped
    --
    MiniDisc_2k2
     
    MiniDisc_2k2, Jul 12, 2003
    #2
    1. Advertising

  3. John Howard

    osmium Guest

    John Howard writes:

    > I'm trying to write a simple press any key routine, but am having some
    > problems. Here is the routine:
    >
    > void PressAnyKey ()
    > {
    > cout << "Press Any Key";
    > do{}while (!kbhit());
    > getch();
    > cout << endl;
    > }
    >
    > When it runs, the next part of the code is to read using a similar

    kbhit(),
    > getch() routine, but it reads in whatever was pressed during the press a
    > key. If I add an extra getch(), then it just sits there until you press

    it
    > twice.


    I guess I don't know what "similar" means. I would expect this code,
    executed twice, to work. There are problems mixing getch() with the
    ordinary I/O libraries. Are you sure you are not tangled up in one of those
    problems? I think you should repost your question, without using the word
    similar, in a group where discussion of getch() is not forbidden.
     
    osmium, Jul 12, 2003
    #3
  4. John Howard

    Evan Guest

    "John Howard" <> wrote in message news:<IPUPa.51517$>...
    > I'm trying to write a simple press any key routine, but am having some
    > problems. Here is the routine:
    >
    > void PressAnyKey ()
    > {
    > cout << "Press Any Key";
    > do{}while (!kbhit());
    > getch();
    > cout << endl;
    > }
    >
    > When it runs, the next part of the code is to read using a similar kbhit(),
    > getch() routine, but it reads in whatever was pressed during the press a
    > key. If I add an extra getch(), then it just sits there until you press it
    > twice.
    >
    > Any ideas?
    >
    > John


    Why don't you get rid of the do-while loop and just have the getch there?
     
    Evan, Jul 12, 2003
    #4
    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. JKop

    Press any key to...

    JKop, Sep 16, 2004, in forum: C++
    Replies:
    2
    Views:
    2,644
    osmium
    Sep 16, 2004
  2. Andrew Robert
    Replies:
    51
    Views:
    22,807
    Mark McIntyre
    Nov 13, 2003
  3. Timur Ametov

    Message "Press any key to continue"

    Timur Ametov, Apr 26, 2006, in forum: C++
    Replies:
    8
    Views:
    473
    Default User
    Apr 27, 2006
  4. Susan Rice
    Replies:
    9
    Views:
    1,007
    Marcus Kwok
    Jul 11, 2006
  5. Replies:
    0
    Views:
    485
Loading...

Share This Page