摘自微软的技术支持论坛
http://social.msdn.microsoft.com/Forums/en/Vsexpressvc/thread/1555ce45-8313-4669-a31e-b95b5d28c787
Alot of users have been asking why their console application terminatesimmediately after running/debugging their program in the Visual C++ IDE. Users claim to see a quick flash of the console applicationand then it disappears.
Thisis due to the fact that when your program is run, it creates a temporaryconsole session to invoke your application. Without any code toexplicitly keep the console open (i.e. requiring that the user enter in a valueusing cin >>), the console terminates immediately after the codecompletes. This is why you see a quick flash of your console application.
Thereare a few things you can do to see the output of your program.
- Start your application without debugging (Ctrl+F5). When doing this, the C++ IDE creates a batch file that executes your code and displays the text "Press any key to continue". This will allow the console to stay on the screen and allow you to view the output.
- Set a breakpoint in the code and run with debugging (F5). When you hit the breakpoint, you can see the console window's current output.
- Run the application from a command prompt. Press Start->All Programs->Visual C++ 2005 Express Edition->Visual Studio Tools->Visual Studio 2005 command prompt. Then navigate to the directory (using 'cd') where the .exe is located. Run the application by typing in the name of the .exe at the command prompt. You will see the output from the program in the command prompt.
Hopefullythis will help people get their first C++ application up and running!
简单的说就是
1 用ctrl + F5 代替 F5
2 在debug模式设置断点
3 手动打开cmd控制台,运行程序
4 在程序结尾加上 system("pause");
第一种和四种比较直接方便