像这样的东西
cout << "Enter the number of columns: " ;
cin >> input ;
while( input != int ){
cout << endl <<"Column size must be an integer"<< endl << endl;
cout << "Enter the number of columns: " ;
cin >> input ;
}
cin
会为你做这件事,有点。cin
如果它收到的东西与以下类型不同,将会失败input
。你可以做的是:
int input;
while(!(cin >> input))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << endl <<"Column size must be an integer"<< endl << endl;
cout << "Enter the number of columns: " ;
}
The cin.clear()
清除错误位,并且cin.ignore()
清除输入流。我在用着number_limits
要获得流的最大大小,这需要您#include<limits>
。或者,您可以只使用一个大数字或一个循环。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)