在要求用户输入时,我不知道如何使用“默认值”。我希望用户能够只按 Enter 并获取默认值。考虑下面的代码,你能帮我吗?
int number;
cout << "Please give a number [default = 20]: ";
cin >> number;
if(???) {
// The user hasn't given any input, he/she has just
// pressed Enter
number = 20;
}
while(!cin) {
// Error handling goes here
// ...
}
cout << "The number is: " << number << endl;