[英]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;
之前return 0;
。 這將使程序等待您在標准輸入(鍵盤)中輸入字符,然后在退出之前按Enter鍵(或僅按Enter鍵)。 (您可能需要在開始時#include <cstdio>
才能使其正常工作)
如果您想真正地變黑,可以在代碼末尾放置一個斷點(您可以在此處學習如何做),但這僅在您處於調試器模式下而不在發行模式下有效。
嘗試放入system("pause");
return 0;
之前return 0;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.