Howdy_McGee
Howdy_McGee

Reputation: 10673

"Press any button to continue" in Visual Studio Express C++?

When creating an console application (blank document) how can I get the "Press Any Button To Continue" to automatically show up?

Upvotes: 0

Views: 402

Answers (4)

Unsigned
Unsigned

Reputation: 9926

There is no builtin function. However you can do a simple loop with kbhit() and getch(), like so:

#include <conio.h>

void main( void )
{
    // Display your message here

    for(;;)
    {
        while( !kbhit() );
        if (getch() == 0x0D)
            break; // Break on ENTER
    }

    // Continue on here
}

Adapted from http://support.microsoft.com/kb/44895

Upvotes: 1

Michael Burr
Michael Burr

Reputation: 340366

When you run a console program in the IDE using "Start Without Debugging" (Ctrl-F5) you get the behavior you're looking for.

For some reason, when you start the program in the IDE under the debugger ("Start Debugging" or plain-old-F5) you don't get that prompt when the program ends. If you just want to be able to see the last bit of what's in the console window when run under the debugger, you can set a breakpoint on the return from main() (or the closing brace for main()).

Upvotes: 2

sj755
sj755

Reputation: 4062

system("pause") is definitely what you were asking for, but using it is very bad practice. Consider just using cin.get() at the end and press enter.

Upvotes: 0

Drahakar
Drahakar

Reputation: 6088

You can add it manually with a system("pause");

*Be carefull, this is not portable (will work on windows, but might not work elsewhere)

Upvotes: 4

Related Questions