Impliment "Press any key to continue" in console program?

Discussion in 'C++' started by Susan Rice, Jul 4, 2006.

  1. Susan Rice

    Susan Rice Guest

    I'm running a simple win32 console application and I want to impliment
    a "Press any key to continue", so I print that prompt, and then
    what's the easiest way to impliment reading any key? Do I use
    'getchar', or 'gets', or some get function? or 'cin', or what's
    an easy way?

    What I want is the program to pause before exiting so the user
    can read whatever messages there are before the console window
    Susan Rice, Jul 4, 2006
    1. Advertisements

  2. try getch, getche, system("PAUSE")

    -- Murali Krishna
    Murali Krishna, Jul 4, 2006
    1. Advertisements

  3. Depending on who uses your program and what it is used for, you might
    not want to *force* your users to do that. They could want to run it
    non-interactively, e.g. in a batch process.

    Consider using a wrapper script that calls your program, then the
    "pause" command or point out the possibility (for Windows users) to
    alter the "Properties > Program > Close on exit" setting.


    Wolfgang Meier, Jul 4, 2006
  4. Susan Rice

    Jim Langston Guest

    Unfortunately, there is no cross platform way to do it. You can use an OS
    specific call ( while ( !kbhit(), getch(), system("pause") etc.. in
    windows ) or use a 3rd party library with such a call ( I seem to recall
    boost has one ). The 3rd party libraries, as I'm aware, just take the their
    function and call the OS specific code anyway.

    If you are not concerned with cross platform operability, then use an OS
    specific call.

    What I do is the following code which is cross platform:

    std::string wait;
    std::cin >> wait;

    but this requires 2 key strokes. Any key and enter. Enter alone won't do
    it. Any other key alone won't do it.

    If your problem is with VC++ closing when you run a program before you can
    see the output, one solution is to press ctrl-f5 to turn it instead of F5,
    then it waits for a keypress before it closes the console.
    Jim Langston, Jul 4, 2006
  5. Susan Rice posted:

    include <cstdio>

    int main()
    std::puts("Press any key to continue...");

    Frederick Gotham, Jul 4, 2006
  6. Susan Rice

    osmium Guest

    That puts a new spin on "any' that even Microsoft hasn't stooped to use.
    osmium, Jul 4, 2006
  7. Susan Rice

    Susan Rice Guest

    How about if I say Press RETURN to exit. Will this work OK?

    void MyExit()
    printf( "Press RETURN to finish:" );
    char c = getchar();

    What I'm finding is I can enter a bunch of characters before pressing
    RETURN, and I'm wondering what happens to all those characters.
    Does just the first character go into c, and the rest are discarded?
    There's no buffer overflow happening is there?
    Susan Rice, Jul 5, 2006
  8. Susan Rice

    Default User Guest

    Default User, Jul 5, 2006
  9. The rest will remain in the input buffer and read by the next
    request for input. In your case, there is no "next request" so the
    remaining input will be discarded.

    Jakob Bieling, Jul 5, 2006
  10. Susan Rice

    Marcus Kwok Guest

    I usually use:

    std::cout << "Press <Enter> to continue...\n";
    std::string trash;
    std::getline(std::cin, trash);
    Marcus Kwok, Jul 11, 2006
    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.