开发者

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

开发者 https://www.devze.com 2023-04-02 19:29 出处:网络
When creating an console application (blank document) how can I get the \"Press Any Button To Continue\" to 开发者_C百科automatically show up?You can add it manually with a

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


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

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


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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号