程序自动退出

program exits automatically

本文关键字:退出 程序      更新时间:2023-10-16

我创建了一个简单的.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;之前