程序自动退出
program exits automatically
我创建了一个简单的.exe文件。当我运行它时,它会自动关闭,而不是说"按任意键退出"。
这是我的程序:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string answer, yes = "yes";
cout << "Is Lucy a top lass ? enter yes or no" << endl;
cin >> answer;
if (answer == yes)
{
cout << "Correctomundo" << endl;
}
else
{
cout << " Blasphemy ! " << endl;
}
return 0;
}
如何让它要求用户在退出之前按任意键?
还有什么方法可以更改它,让它说其他内容而不是"按任意键退出"?
在
代码末尾添加std::getchar();
,紧return 0;
.这将使程序等待您从标准输入(键盘)中输入字符并在退出之前按回车键(或直接按回车键)。(不过,您可能需要在开始时#include <cstdio>
才能使其正常工作)
如果你想变得非常黑客,你可以在代码的末尾放置一个断点(你可以在这里学习如何做到这一点),但这只有在你处于调试器模式而不是发布模式时才有效......
尝试放置system("pause");
在return 0;
之前
相关文章:
- 为什么异常不退出程序?
- 虽然循环在使用回车键时不退出程序
- 如果 I/O read() 处于阻塞阶段,如何使用 Ctrl+C 退出 C++ 程序?
- 捕获异常后如何退出程序执行
- 在C++中使用exit()退出程序有问题吗
- 从函数打印 CLI 帮助消息后,我应该如何干净地退出 C++ 程序?
- 如何实现否定用户输入退出程序和打印列表?
- gdb 退出:程序接收信号 SIGILL,使用 gcc / intel_mkl / gfortran -mavx 编译的
- 试图调节我的 do-while 以停止在终端中退出程序.菜单不会根据需要循环使用当前语句
- 如何在C 中按ENTER/返回键时退出程序循环
- 退出程序,默认开关
- 我想让程序保持运行,直到用户输入 n 退出程序
- 我找不到第一个用户输入后立即退出程序的原因
- 安全地退出程序 Win32 C++从分离的 std::线程
- 如何使用QCloseEvent退出程序
- 在主之前退出程序是明确的行为吗?
- C++键盘挂钩-退出程序,但也防止密钥在其他地方被处理
- 在类中运行析构函数时退出程序
- 由于删除 [] 而未退出程序
- 输出缓冲区和Cntrl+C以退出程序