C++ Visual Studio 2010“按任意键继续......” 不显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9175627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 12:25:43  来源:igfitidea点击:

Visual Studio 2010 "Press any key to continue . . ." does not show

c++visual-studio-2010

提问by not_so_secret_agent

"Press any key to continue" wordings do not show in console when my app finishes execution.

当我的应用程序完成执行时,控制台中不会显示“按任意键继续”字样。

I know this prompt should be showing in "start without debugging" mode, but it does not! As soon as I enter any parameter and hit enter, console window closes without showing the output...

我知道这个提示应该显示在“启动而不调试”模式下,但它没有!只要我输入任何参数并按回车键,控制台窗口就会关闭而不显示输出...

What should I do to make the console app prompt me for any key when it finishes execution?

我应该怎么做才能让控制台应用程序在完成执行时提示我输入任何键?

回答by Dialecticus

According to comments about a VS2010 bug, you should get desired behavior if you set the subsystem property of the project to "console". Go to menu Project > Properties > in new window select Configuration Properties > Linker > System > property SubSystem should have the value Console (/SUBSYSTEM:CONSOLE)

根据关于 VS2010 bug 的评论,如果将项目的子系统属性设置为“控制台”,您应该会获得所需的行为。转到菜单 Project > Properties > 在新窗口中选择 Configuration Properties > Linker > System > property SubSystem 应该具有值Console (/SUBSYSTEM:CONSOLE)

回答by RB.

In your mainmethod, add a Try...Catch...Finallyblock as shown:

在您的main方法中,添加一个Try...Catch...Finally块,如下所示:

This will ensure that any exceptions you are getting will be displayed, and that the user will have to take action to close the command window.

这将确保您获得的任何异常都将显示出来,并且用户将必须采取措施关闭命令窗口。

try 
{
    // Your existing code
}
catch (Exception e)
{
    // Log the exception, e.g:
    Console.WriteLine(e.ToString());
}
finally
{
    Console.WriteLine ("Please press any key to close");
    Console.ReadKey();
}