假设您希望用户输入根的基数,以及根的另一个数字。但是,如果用户仅提供 1 个数字,则第二个输入将默认为 2,即平方根。
我想知道如果只输入一个数字后按“enter”键,如何停止输入。
我正在使用c++。
最佳答案
您可以尝试以下代码:
#include <iostream>
using namespace std;
int main() {
int x, y;
int sum;
cout << "Type a number: ";
cin >> x;
cout << "Type another number: ";
cin >> y;
sum = x + y;
cout << "Sum is: " << sum;
return 0;
}
关于c++ - 如何让用户输入一个或两个输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72210742/