我开始在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;
}
我漏了哪一行?
当前回答
另一种选择是使用
#include <process.h>
system("pause");
虽然这不是很可移植,因为它只能在Windows上工作,但它会自动打印
按任意键继续…
其他回答
实际上,真正的解决方案是项目模板本身的选择。 你必须在旧的VS中选择Win32控制台应用程序,或者先填写项目名称,然后双击Windows桌面向导,然后选择Win32控制台应用程序。然后在此时选择空项目。然后,这允许原始提问者真正想要的东西,而无需添加额外的停止点和保持代码。 我也遇到过这个问题。答案也在MSDN网站上。
你可以把 keep_window_open (); 在返回之前,这里有一个例子
int main()
{
cout<<"hello world!\n";
keep_window_open ();
return 0;
}
就在你的include之后 YW std:: cin.clear ();//重置任何错误标志 std:: cin.ignore (std:: numeric_limitsstd:: streamsize:: max (), ' \ n ');//忽略输入缓冲区中的任何字符,直到找到一个输入字符 std:: cin.get ();//从用户那里再获取一个字符
在main()的结束大括号上放置断点。即使有多个return语句,它也会被绊倒。唯一的缺点是对exit()的调用不会被捕获。
如果你没有调试,按照Zoidberg回答中的建议,用Ctrl+F5来启动你的程序,而不是只用F5。
用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提供)