如何阻止 C++ 控制台应用程序立即退出?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2529617/
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 stop C++ console application from exiting immediately?
提问by Adam P
Lately, I've been trying to learn C++ from this website. Unfortunately whenever I try to run one of the code samples, I see that program open for about a half second and then immediately close. Is there a way to stop the program from closing immediately so that I can see the fruits of my effort?
最近,我一直在尝试从这个网站学习 C++ 。不幸的是,每当我尝试运行其中一个代码示例时,我都会看到该程序打开了大约半秒钟,然后立即关闭。有没有办法阻止程序立即关闭,以便我可以看到我的努力成果?
采纳答案by James McNellis
Edit:As Charles Bailey rightly points out in a comment below, this won't work if there are characters buffered in stdin
, and there's really no good way to work around that. If you're running with a debugger attached, John Dibling's suggested solutionis probably the cleanest solution to your problem.
编辑:正如 Charles Bailey 在下面的评论中正确指出的那样,如果有字符缓冲在stdin
. 如果您使用附加的调试器运行,John Dibling 建议的解决方案可能是解决您问题的最干净的解决方案。
That said, I'll leave this here and maybe someone else will find it useful. I've used it a lot as a quick hack of sorts when writing tests during development.
也就是说,我会把它留在这里,也许其他人会发现它很有用。在开发过程中编写测试时,我经常使用它作为各种快速技巧。
At the end of your main
function, you can call std::getchar();
在你的main
函数结束时,你可以调用std::getchar();
This will get a single character from stdin
, thus giving you the "press any key to continue" sort of behavior (if you actually want a "press any key" message, you'll have to print one yourself).
这将从 获得单个字符stdin
,从而为您提供“按任意键继续”的行为(如果您确实想要“按任意键”消息,则必须自己打印一个)。
You need to #include <cstdio>
for getchar
.
你需要#include <cstdio>
为getchar
.
回答by nabulke
If you are using Visual Studio and you are starting the console application out of the IDE:
如果您使用的是 Visual Studio 并且正在从 IDE 启动控制台应用程序:
pressing CTRL-F5(start without debugging) will start the application and keep the console window open until you press any key.
按CTRL-F5(不调试启动)将启动应用程序并保持控制台窗口打开,直到您按任意键。
回答by codaddict
The solution by James works for all Platforms.
James 的解决方案适用于所有平台。
Alternatively on Windows
you can also add the following just before you return from main
function:
或者,Windows
您还可以在从main
函数返回之前添加以下内容:
system("pause");
This will run the pause
command which waits till you press a key and also displays a nice message Press any key to continue . . .
这将运行pause
命令,直到您按下一个键并显示一条不错的消息Press any key to continue . . .
回答by Mr. Underhill
If you are using Microsoft's Visual C++ 2010 Express and run into the issue with CTRL+F5not working for keeping the console open after the program has terminated, take a look at this MSDN thread.
如果您使用 Microsoft 的 Visual C++ 2010 Express 并遇到CTRL+F5在程序终止后无法保持控制台打开的问题,请查看此 MSDN 线程。
Likely your IDE is set to close the console after a CTRL+F5 run; in fact, an "Empty Project" in Visual C++ 2010 closes the console by default. To change this, do as the Microsoft Moderator suggested:
可能您的 IDE 设置为在 CTRL+F5 运行后关闭控制台;事实上,Visual C++ 2010 中的“空项目”默认关闭控制台。要更改此设置,请按照 Microsoft 版主的建议进行操作:
Please right click your project name and go to Properties page, please expand Configuration Properties -> Linker -> System, please select Console (/SUBSYSTEM:CONSOLE) in SubSystem dropdown. Because, by default, the Empty project does not specify it.
请右键单击您的项目名称并转到“属性”页面,请展开配置属性 -> 链接器 -> 系统,请在子系统下拉列表中选择控制台 (/SUBSYSTEM:CONSOLE)。因为,默认情况下,Empty 项目没有指定它。
回答by John Dibling
I usually just put a breakpoint on main()
's closing curly brace. When the end of the program is reached by whatever means the breakpoint will hit and you can ALT-Tab to the console window to view the output.
我通常只是在main()
的右花括号上放置一个断点。当通过任何方式到达程序末尾时,断点将被击中,您可以按 ALT-Tab 到控制台窗口查看输出。
回答by Amos
Why not just run the program from a console ie run the program from cmd.exe if you're using Windows. That way the window stays open after the program finishes.
如果您使用的是 Windows,为什么不直接从控制台运行该程序,即从 cmd.exe 运行该程序。这样窗口在程序完成后保持打开状态。
[EDIT]: When I use KDevelop4 there is a fully fledged instance of Bash (a Linux CLI) running in a tab at the bottom of the IDE. Which is what I use in these sort of circumstances.
[编辑]:当我使用 KDevelop4 时,有一个完全成熟的 Bash 实例(一个 Linux CLI)在 IDE 底部的选项卡中运行。这就是我在这种情况下使用的。
回答by robert
Before the end of your code, insert this line:
在代码结束之前,插入以下行:
system("pause");
This will keep the console until you hit a key.
这将保持控制台,直到您按下一个键。
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
cout << "Please enter your first name followed by a newline\n";
cin >> s;
cout << "Hello, " << s << '\n';
system("pause"); // <----------------------------------
return 0; // This return statement isn't necessary
}
回答by tsx
Call cin.get();
2 times:
调用cin.get();
2 次:
//...
cin.get();
cin.get();
return 0
}
回答by tsx
If you run your code from a competent IDE, such as Code::Blocks, the IDE will manage the console it uses to run the code, keeping it open when the application closes. You don't want to add special code to keep the console open, because this will prevent it functioning correctly when you use it for real, outside of the IDE.
如果您从合适的 IDE(例如Code::Blocks )运行代码,IDE 将管理它用来运行代码的控制台,并在应用程序关闭时保持打开状态。您不想添加特殊代码来保持控制台打开,因为当您在 IDE 之外实际使用它时,这将阻止它正常运行。
回答by Polaris878
Okay I'm guessing you are on Windows using Visual Studio... why? Well because if you are on some sort of Linux OS then you'd probably be running it from the console.
好吧,我猜你在使用 Visual Studio 的 Windows 上......为什么?好吧,因为如果您使用某种 Linux 操作系统,那么您可能会从控制台运行它。
Anyways, you can add crap to the end of your program like others are suggesting, or you can just hit CTRL + F5(start without debugging) and Visual Studio will leave the console up once complete.
无论如何,您可以像其他人建议的那样在程序末尾添加废话,或者您只需按CTRL + F5(无需调试即可启动),Visual Studio 将在完成后离开控制台。
Another option if you want to run the Debug version and not add crap to your code is to open the console window (Start -> Run -> cmd) and navigate to your Debug output directory. Then, just enter the name of your executable and it will run your debug program in the console. You can then use Visual Studio's attach to process or something if you really want to.
如果您想运行 Debug 版本而不在代码中添加废话,另一种选择是打开控制台窗口(开始 -> 运行 -> cmd)并导航到您的Debug 输出目录。然后,只需输入可执行文件的名称,它就会在控制台中运行您的调试程序。然后,您可以使用 Visual Studio 的附加功能进行处理,或者如果您真的愿意的话。