如何防止输出屏幕在 Visual Studio 2013 C++ Compiler 中消失
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20736648/
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
How to prevent output screen from disappear in Visual Studio 2013 C++ Compiler
提问by user3128376
I just downloaded Visual Studio 2013. When I compile C, it doesn't show me my output. The output screen will show up for a brief second and then disappears.
我刚刚下载了 Visual Studio 2013。当我编译 C 时,它没有显示我的输出。输出屏幕将显示一秒钟,然后消失。
#include <stdio.h>
int main()
{
printf("hi");
return 0;
}
"The program '[5688] Project1.exe' has exited with code 0 (0x0)." I know my code works and run correctly except I just can't make the output screen stay on without exiting after a second.
“程序‘[5688] Project1.exe’已退出,代码为 0 (0x0)。” 我知道我的代码可以正常工作并且可以正常运行,但我无法在不退出的情况下使输出屏幕保持打开状态。
回答by
You can run the application in debug mode and in release mode. Normally Ctrl + F5
will run the application without debugger. And F5
just runs the application.
您可以在调试模式和发布模式下运行应用程序。通常Ctrl + F5
会在没有调试器的情况下运行应用程序。并且F5
只运行应用程序。
If you do Ctrl+F5
("Start without Debugging"), the console remains open at the end and asks you to Press any key to continue . . .
here you can see the output.
如果你这样做Ctrl+F5
(“开始而不调试”),控制台在最后保持打开状态并要求你在Press any key to continue . . .
这里你可以看到输出。
If you are just using F5
then you are in a debug mode. At the end you add, getchar()
function before retuen 0;
so the console will wait until you press any key...
如果您只是在使用,F5
那么您处于调试模式。最后你添加,getchar()
function beforeretuen 0;
所以控制台会等到你按任意键......
回答by jpw
Another option in addition to what's already been mentioned is to go into the properties for the project and change the Subsystem
in the System
section in the Linker
options to Console (/SUBSYSTEM:CONSOLE)
. Then the console window will remain when you run the program using ctrl+f5
(Debug/Start without debugging).
除了什么已经提到的另一种选择是进入项目的性质,改变Subsystem
在System
一节中Linker
的选项Console (/SUBSYSTEM:CONSOLE)
。然后,当您使用ctrl+f5
(Debug/Start without debugging)运行程序时,控制台窗口将保留。
回答by Baqer Naqvi
add this code before return 0
;
在此之前添加此代码return 0
;
int num;
scanf ("%d",&num);
or
或者
getchar();
回答by Fiddling Bits
There's several things you can do (I'm assuming you're using Windows):
您可以做几件事(我假设您使用的是 Windows):
- Compile and execute your program using the
Visual Studio Command Prompt
program. - Add
getchar();
before returning to the OS. - Add
system("pause");
before returning to the OS.
- 使用该
Visual Studio Command Prompt
程序编译并执行您的程序。 getchar();
在返回操作系统之前添加。system("pause");
在返回操作系统之前添加。
回答by james
#include <stdlib.h>
#include <stdio.h>
int main()
{
printf("hello world");
system("pause"); //this pauses the program until you press any key
return 0;
}
the output will be:
输出将是:
hello world
你好,世界
press any key to continue ...
按任意键继续 ...
回答by MSalters
I just put a breakpoint (F9 key) on the return 0
statement. Works only in debugging mode, but that's precisely what you want. If you run the program directly from the command line, it already works as intended.
我只是在return 0
语句上放置了一个断点(F9 键)。仅在调试模式下有效,但这正是您想要的。如果您直接从命令行运行该程序,它已经按预期运行。
回答by Debendra Dash
To keep your screen from closing you can use getchar() as follow in Visual studio:
为了防止屏幕关闭,您可以在 Visual Studio 中使用 getchar() 如下:
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
cout << "Hello\n";
getchar();
}
回答by Marcus Lobo
You can also hold CTRL + F5 to get the window to stay open.
您也可以按住 CTRL + F5 使窗口保持打开状态。
回答by Duong Trung Nghia
I use Visual Studio 2013 for Python and I also struggle with that problem. My solution is to press F5
instead of Ctrl + F5
, then I will have 2 pop-up windows (console and program output).
我使用 Visual Studio 2013 for Python,我也遇到了这个问题。我的解决方案是按F5
而不是Ctrl + F5
,然后我将有 2 个弹出窗口(控制台和程序输出)。
I close the console window and the other will be closed together.
我关闭控制台窗口,另一个将一起关闭。
回答by gorilon
I first used the metioned getchar() and breakpoints solutions but this is no good if you want the program to end (for example if you are using a memory leak detector). I got over this by redirecting the output to a file. You can do this by inserting >output.txt
in the command line option under the debug section of project properties
我首先使用了提到的 getchar() 和 breakpoints 解决方案,但是如果您希望程序结束(例如,如果您使用内存泄漏检测器),这并不好。我通过将输出重定向到一个文件来解决这个问题。您可以通过>output.txt
在项目属性的调试部分下的命令行选项中插入来执行此操作