我开始在Visual c++和我想知道如何保持控制台窗口。
例如,这将是一个典型的“hello world”应用程序:
int _tmain(int argc, _TCHAR* argv[])
{
cout << "Hello World";
return 0;
}
我漏了哪一行?
我开始在Visual c++和我想知道如何保持控制台窗口。
例如,这将是一个典型的“hello world”应用程序:
int _tmain(int argc, _TCHAR* argv[])
{
cout << "Hello World";
return 0;
}
我漏了哪一行?
当前回答
在返回行上放置断点。
你在调试器中运行它,对吧?
其他回答
我的观点:
选择1:在main()的末尾添加断点
选择2:在返回0之前添加这段代码:
std::cout << "Press ENTER to continue..."; //So the User knows what to do
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
对于std::numeric_limits,您需要包含<iomanip>
用Ctrl+F5启动项目,而不是只用F5。
控制台窗口现在将保持打开,按任意键继续…程序退出后的消息。
注意,这需要Console(/子系统:Console)链接器选项,您可以如下方式启用该选项:
Open up your project, and go to the Solution Explorer. If you're following along with me in K&R, your "Solution" will be 'hello' with 1 project under it, also 'hello' in bold. Right click on the 'hello" (or whatever your project name is.) Choose "Properties" from the context menu. Choose Configuration Properties>Linker>System. For the "Subsystem" property in the right-hand pane, click the drop-down box in the right hand column. Choose "Console (/SUBSYSTEM:CONSOLE)" Click Apply, wait for it to finish doing whatever it does, then click OK. (If "Apply" is grayed out, choose some other subsystem option, click Apply, then go back and apply the console option. My experience is that OK by itself won't work.)
CTRL-F5和子系统提示一起工作;它们不是独立的选项。
(来自http://social.msdn.microsoft.com/Forums/en-US/vcprerelease/thread/21073093-516c-49d2-81c7-d960f6dc2ac6的DJMorreTX提供)
正如一些人已经指出的那样,zoidberg的解决方案没有附加调试器,这是您通常不希望看到的。
在我看来,最好的选择是相应地配置你的VS(从VS 2017开始),通过到工具>选项>调试>通用。在这里,您取消选中“调试停止时自动关闭控制台”(在最底部),在您的情况下,可能选中了该选项。
你可以把 keep_window_open (); 在返回之前,这里有一个例子
int main()
{
cout<<"hello world!\n";
keep_window_open ();
return 0;
}
(有些选项可能被称为不同的名称。我不使用英文版)
我有同样的问题,当我创建项目的选项“空项目”,创建项目为“win32控制台应用程序”而不是“空项目”。在现在弹出的对话框中,你按“继续”,之后你可以勾选“空项目”选项并按确认。然后按CTRL + F5将打开一个控制台,但不会自动关闭。