我刚开始学习 C++,我根据课本写了这个示例程序,当我编译并运行它时,它只是在用户输入任意数字并按回车键后关闭。我猜这个问题的答案很明显,所以请原谅我这个新手....这真的是我的第一个 C++ 程序 :P
#include <iostream>
using namespace std;
int main ()
{
int numberOfLanguages;
cout << "Hello Reader.\n"
<< "Welcome to C++.\n"
cout << "How many programming languages have you used? ";
cin >> numberOfLanguages;
if(numberOfLanguages < 1)
cout << "Read the preface. You may prefer.\n"
<< "a more elementary book by the same author.\n";
else
cout << "Enjoy the book.\n";
return 0;
}
最佳答案
假设您正在设计一个应用程序执行模型。您有两个选择:
A) 当到达程序末尾时,程序将终止。
B) 当一个程序到达结尾时,该程序将以某种奇怪的边缘状态保持事件状态。它仍将保留系统资源,实际上不会做任何事情,但为了关闭用户必须明确终止它。
我认为任何人都会在这里选择选项 A,这就是您所看到的。 main
结束,您的程序退出。
如果您希望它在最后暂停,请从用户那里获取一些输入,即
char c;
std::cin >> c;
return 0;
关于c++ - 非常基本的 C++ 程序在用户输入后无特殊原因关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6560625/