我不知道如何在询问用户输入时如何使用“默认值”。我希望用户能够按Enter键并获取默认值。考虑下面的一段代码,你能帮助我吗?用户输入(cin) - 默认值
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;
是否有任何方法来检查用户是否输入有效的输入(如在cin中)。 我的意思是,我想要检测用户输入某些字符而不是数字并输出错误信息。 – tumchaaditya 2012-04-26 02:44:36