我开始在Visual c++和我想知道如何保持控制台窗口。

例如,这将是一个典型的“hello world”应用程序:

int _tmain(int argc, _TCHAR* argv[])
{
    cout << "Hello World";
    return 0;
}

我漏了哪一行?


当前回答

只需在代码的末尾添加system("pause"),然后像这样返回0

#include <stdlib.h>

int main()
{
    //some code goes here
    system("pause")
    return 0;
}

其他回答

你可以使用cin.get();或cin.ignore ();在return语句之前,避免控制台窗口关闭。

标准的方法是在return语句之前使用cin.get()。

int _tmain(int argc, _TCHAR* argv[])
{
    cout << "Hello World";
    cin.get();
    return 0;
}

正如一些人已经指出的那样,zoidberg的解决方案没有附加调试器,这是您通常不希望看到的。

在我看来,最好的选择是相应地配置你的VS(从VS 2017开始),通过到工具>选项>调试>通用。在这里,您取消选中“调试停止时自动关闭控制台”(在最底部),在您的情况下,可能选中了该选项。

sin .get()或system("PAUSE")。 我没听说过你可以用return(0);

我也有同样的问题;在我的应用程序中有多个exit()点,没有办法知道它到底在哪里退出,然后我发现了这一点:

atexit(system("pause"));

or

atexit(cin.get());

这样,无论我们退出程序的哪个位置,它都会停止。