检查输入
cin会检查输入格式,输入与预期格式不符时,会返回false.
cout << "Enter numbers: ";
int sum = 0;
int input;
while (cin >> input)
sum += input;
cout << "Last value entered = " << input << endl;
cout << "Sum = " << sum << endl;

上面的检查可以放入try、catch语句中。
try{
while (cin >> input)
sum += input;
}catch(ios_base::failure &f){
cout<<f.what()<<endl;
}
字符串输入:getline()、get()、ignore()
getline()读取整行,读取指定数目的字符或遇到指定字符(默认换行符)时停止读取。 </