这可能是一个令人尴尬的问题,因为答案无疑是显而易见的。

我已经使用Visual Studio很多年了,但这是我第一次做任何“控制台应用程序”开发。

当我运行我的应用程序时,控制台窗口弹出,程序输出出现,然后当应用程序退出时窗口关闭。

是否有一种方法可以让它一直打开直到我检查输出,或者在窗口关闭后查看结果?


当前回答

您也可以使用此选项

#include <conio.h> 
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main() {
   .
   .
   .
   getch(); 

   return 0;
}

其他回答

右键单击您的项目

属性>配置属性>连接器>系统

在子系统选项中选择控制台(/子系统:控制台),或者你可以在文本字段中输入控制台!

现在试试……应该可以

一个更好的解决方案:

atexit([] { system("PAUSE"); });

在程序开始的时候。

优点:

可以使用std::exit() 可以从主有多个返回 您可以在调试器下运行程序 IDE独立(+ OS独立,如果你使用cin.sync();cin.ignore ();Trick而不是system("pause");)

缺点:

必须修改代码 不会暂停std::terminate() 仍然会发生在IDE/调试器会话之外的程序中;你可以在Windows下使用:


extern "C" int __stdcall IsDebuggerPresent(void);
int main(int argc, char** argv) {
    if (IsDebuggerPresent())
        atexit([] {system("PAUSE"); });
    ...
}

Goto Debug菜单->按下StartWithoutDebugging

只需要在最后一行代码中输入:

system("pause");

从Visual Studio 2017(15.9.4)开始,有一个选项:

Tools->Options->Debugging->Automatically close the console

对应的片段来自Visual Studio文档:

当调试停止时自动关闭控制台: 告诉Visual Studio在调试会话结束时关闭控制台。