#include <iostream>
#include <string>
using namespace std;
{
cin.clear(); //清除错误
char c;
while(cin >> c) //查看字符
{
if(isdigit(c)) //字符是数字
{
cin.unget(); //把字符放回去
return;
}
}
{
int n = 0;
while(true)
{
if(cin >> n) return n;
cout << "\n Sorry, that not an integer, please try again . \n";
skip_to_int();
}
{
cout << tip;
int n = 0;
while(true)
{
n = get_int();
if(n >= low && n <= high) return n;
cout << sorry;
}
}
int main()
{
int n = get_int(1, 10,
" Please input an integer in the range 1 to 10 : ",
"\n Sorry , The number is not in [0-10] ! please try again . \n\n");
cout << " Ok , the number is " << n;
cin.get();
return 0;
}
#include <string>
using namespace std;
{
if(cin.fail()) //输入不是数字,发生错误{
cin.clear(); //清除错误
char c;
while(cin >> c) //查看字符
{
if(isdigit(c)) //字符是数字
{
cin.unget(); //把字符放回去
return;
}
}
}
cerr << "\nBad Input ! \n" << endl;
}
//输入数字
{
int n = 0;
while(true)
{
if(cin >> n) return n;
cout << "\n Sorry, that not an integer, please try again . \n";
skip_to_int();
}
}
//输入low到high之间的数字,tip:提示,sorry:错误提示
{
cout << tip;
int n = 0;
while(true)
{
n = get_int();
if(n >= low && n <= high) return n;
cout << sorry;
}
}
int main()
{
int n = get_int(1, 10,
" Please input an integer in the range 1 to 10 : ",
"\n Sorry , The number is not in [0-10] ! please try again . \n\n");
cout << " Ok , the number is " << n;
cin.get();
return 0;
}