Hi,
You may simply check that input is a valid
unsigned int
and nothing else, for instance:
#include <iostream>
#include <limits>
using std::cin;
using std::cout;
using std::endl;
unsigned int IdInput()
{
unsigned int id = 0;
for(;;)
{
cout <<"Enter Your id: ";
cin >> id;
cin.clear();
cin.ignore(std::numeric_limits<int>::max(), '\n');
if (cin.gcount() != 1)
cout<<"Your id must be digit"<< endl;
else if (!id || (id > 32787))
cout << "Your id is wrong.. " << endl;
else
return id;
}
}
int main()
{
cout << "Valid id : " << IdInput() << endl;
}
Code edited to handle input beginning with a non-digit.
cheers,
AR