我开始在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;
}
我漏了哪一行?
当前回答
用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提供)
其他回答
下面是一种不修改任何代码而保持命令窗口打开的方法:
在Visual Studio中,打开项目属性页-> Debugging。
命令:输入$(ComSpec)
命令参数:输入/k $(TargetPath)。将任何参数附加到您自己的应用程序。
现在F5或Ctrl-F5在一个新窗口中执行Windows/System32/cmd.exe,并且/k确保在执行完成后命令提示符保持打开。
缺点是执行不会在断点处停止。
只需要在main的最后一个花括号上加一个断点。
int main () {
//...your code...
return 0;
} //<- breakpoint here
它适合我,不需要调试就可以运行。它还会在到达断点之前执行析构函数,这样您就可以检查在这些析构函数上打印的消息(如果有的话)。
就在你的include之后 YW std:: cin.clear ();//重置任何错误标志 std:: cin.ignore (std:: numeric_limitsstd:: streamsize:: max (), ' \ n ');//忽略输入缓冲区中的任何字符,直到找到一个输入字符 std:: cin.get ();//从用户那里再获取一个字符
你可以使用cin.get();或cin.ignore ();在return语句之前,避免控制台窗口关闭。
我也有同样的问题。我在return语句之前使用了_getch()。它的工作原理。