if i declare a unsigned int variable, and set a test loop to test if input correct. and when i type in a negative number, the loop still see it as input right. how can i make such a testing loop, which i declare a unsigned int, and when type in a negative number it tells wrong.
my invalide code following:
1 2 3 4 5 6 7 8 9 10 11
int main()
{
unsignedint i;
while (!(cin >> i))
{
cout << "Wrong." << i << endl;
cin.clear();
while (cin.get() != '\n');
}
system("pause");
}
First get the value using a string, check the first character in the string. If it is a '-', then do not accept, otherwise convert it to unsigned using this guide: