程序自动退出

时间:2016-03-07 05:27:43

标签: c++ visual-studio runtime-error

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

如何在退出前让用户按任意键?

还有什么方法可以改变它,所以它说了别的而不是“按任意键退出”?

2 个答案:

答案 0 :(得分:1)

std::getchar();之前的代码末尾添加return 0;。这将使程序等待您从标准输入(键盘)输入一个字符,然后在退出之前按Enter键(或只需按Enter键)。 (您可能需要在开始时#include <cstdio>才能使其工作)

如果你想变得非常hacky,你可以在你的代码结尾处放一个断点(你可以学习如何做here),但这只能在你处于调试器模式时工作,而不是在发布模式......

答案 1 :(得分:0)

尝试放system("pause");return 0;

之前