Visual C++ 输出退出如此之快
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12226148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Visual C++ output exits so fast
提问by Lemon Juice
Possible Duplicate:
Visual Studio terminates my console application too fast
I am 100% new to Visual Studio C++. I am new to C++ too and I used netbeans up to now, with cygwin. Today I installed visual C++ 2012 and ran the following simple code
我 100% 是 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;
}
The console window came, and it went so fast without giving me a chance to see the output too, mentioning the following.
控制台窗口来了,它运行得如此之快,我也没有机会看到输出,提到以下内容。
'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).
I browsed the internet and some have suggested to set Project Properties > Configuration Properties > Linker > Debugging tab > Generate Debug info to "yes". This is set to yes already, but still no good. I am planning to work with opencv with this IDE too. So Please help!!!
我浏览了互联网,有些人建议将项目属性 > 配置属性 > 链接器 > 调试选项卡 > 生成调试信息设置为“是”。这已经设置为是,但仍然不好。我也计划在这个 IDE 上使用 opencv。所以请帮忙!!!
回答by Infiltrator
EDIT: You obviously shouldn't put in a cin
into a real program unless you actually want to read in input.
编辑:你显然不应该把 acin
放入一个真正的程序中,除非你真的想读入输入。
What's happening is that your program is printing "Hello World", as you told it to, and then finishing.
发生的事情是你的程序正在打印“Hello World”,正如你告诉它的那样,然后完成。
VS, being a MS program, only creates a terminal for your program until it finishes, whereupon it closes the terminal.
VS 是一个 MS 程序,它只为您的程序创建一个终端,直到它完成,然后关闭终端。
In order to see the output, you can put this after your cout:
为了查看输出,您可以将其放在 cout 之后:
string buffer;
cin >> buffer;
This will wait for you to press enter before terminating the program.
这将等待您在终止程序之前按 Enter。
回答by CygnusX1
If you run the program separately from the IDE (through Ctrl+F5 instead of F5), it will not close the console window at the end but will print "Press any key to continue..." (without you modifying the source code in any way).
如果您与 IDE 分开运行该程序(通过 Ctrl+F5 而不是 F5),它最终不会关闭控制台窗口,但会打印“按任意键继续...”(无需修改源代码)反正)。
The down side of this approach is that all assertions and failures will lead to program crash, rather than being caught by the Visual Studio.
这种方法的缺点是所有断言和失败都会导致程序崩溃,而不是被 Visual Studio 捕获。
回答by CygnusX1
That is the normal behaviour for console applications that are called without an open console. For example, go to your system32 folder and double-click attrib.exe
.
这是在没有打开控制台的情况下调用的控制台应用程序的正常行为。例如,转到您的 system32 文件夹并双击attrib.exe
。
The easiest way to see the output is to set a breakpoint at the last line in your program.
查看输出的最简单方法是在程序的最后一行设置断点。
回答by Science_Fiction
It's classed as a "hack" but for such a simple app like your's you could just use the line:
它被归类为“黑客”,但对于像您这样简单的应用程序,您可以使用以下行:
system(“pause”);
It's platform specific and slow but seems you are on Windows.
它特定于平台且速度缓慢,但似乎您使用的是 Windows。
A Breakpoint is also a good way to pause execution. Once Main has executed your application is done, and that is what you are witnessing. Put a breakpoint on the last curly brace of Main.
断点也是暂停执行的好方法。一旦 Main 执行了您的应用程序就完成了,这就是您所看到的。在 Main 的最后一个花括号上放置一个断点。
char a = getchar();
Will pause and wait for you to enter a character.
将暂停并等待您输入字符。
回答by fasked
If you make console application it was to be logical - try to run your program from console.
如果您制作控制台应用程序,那是合乎逻辑的 - 尝试从控制台运行您的程序。
回答by relaxxx
You can place a breakpoint at the end of your main, or call getchar()
, or system("pause")
or execute your program through console
您可以在 main 的末尾放置一个断点,或者调用getchar()
,或者 system("pause")
通过控制台执行您的程序
when setting breakpoint, make sure you are in debug mode
设置断点时,请确保您处于调试模式