Visual C ++输出退出的速度非常快

时间:2012-09-01 08:21:19

标签: c++ visual-studio-2010 visual-studio visual-c++ visual-c++-2010

  

可能重复:
  Visual Studio terminates my console application too fast

我是Visual Studio C ++的新手。我也是C ++的新手,到目前为止,我使用netbeans,使用cygwin。今天我安装了visual C ++ 2012并运行了以下简单代码

// HelloWorld.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

using namespace std;


int main()
{
    cout << "Hello World" << endl;
}

控制台窗口来了,它的速度非常快,没有让我有机会看到输出,提到以下内容。

'HelloWorld.exe': Loaded 'C:\Users\yohan\Documents\Visual Studio 2010\Projects\HelloWorld\Debug\HelloWorld.exe', Symbols loaded.
'HelloWorld.exe': Loaded 'C:\Windows\System32\ntdll.dll', Cannot find or open the PDB file
'HelloWorld.exe': Loaded 'C:\Windows\System32\kernel32.dll', Cannot find or open the PDB file
'HelloWorld.exe': Loaded 'C:\Windows\System32\KernelBase.dll', Cannot find or open the PDB file
'HelloWorld.exe': Loaded 'C:\Windows\System32\msvcp100d.dll', Symbols loaded.
'HelloWorld.exe': Loaded 'C:\Windows\System32\msvcr100d.dll', Symbols loaded.
The program '[3436] HelloWorld.exe: Native' has exited with code 0 (0x0).

我浏览了互联网,有些人建议设置项目属性&gt;配置属性&gt;链接器&gt;调试选项卡&gt;生成调试信息为“是”。这已经设置为是,但仍然没有好处。我打算在这个IDE上使用opencv。所以请帮忙!!!

6 个答案:

答案 0 :(得分:4)

编辑:你显然不应该将cin放入真实的程序,除非你真的想读入输入。

正在发生的事情是你的程序正在打印&#34; Hello World&#34;,正如你告诉它的那样,然后完成。

VS,作为一个MS程序,只为你的程序创建一个终端,直到它完成,然后关闭终端。

为了查看输出,你可以把它放在你的cout之后:

string buffer;
cin >> buffer;

这将等待您在终止程序之前按Enter键。

答案 1 :(得分:4)

如果您从IDE单独运行程序(通过Ctrl + F5而不是F5),它将不会在结束时关闭控制台窗口,但会打印“按任意键继续......”(不修改任何方式的源代码。)

这种方法的缺点是所有断言和失败都会导致程序崩溃,而不是被Visual Studio捕获。

答案 2 :(得分:2)

这是在没有打开控制台的情况下调用的控制台应用程序的正常行为。例如,转到system32文件夹,然后双击attrib.exe

查看输出的最简单方法是在程序的最后一行设置断点。

答案 3 :(得分:0)

它被归类为“黑客”,但对于像你这样的简单应用你可以使用这条线:

system(“pause”);

它的平台特定而且速度很慢但似乎你在Windows上。

断点也是暂停执行的好方法。一旦Main执行完您的应用程序就完成了,这就是您正在目睹的事情。在Main的最后一个大括号上放置一个断点。

char a = getchar();

将暂停并等待您输入角色。

答案 4 :(得分:0)

如果您创建控制台应用程序,那就是合乎逻辑的 - 尝试从控制台运行程序。

答案 5 :(得分:0)

您可以在主页的末尾放置一个断点,或者拨打getchar()system("pause")或通过控制台执行您的程序

设置断点时,请确保您处于调试模式