int num = 0;
while(true){
cout << "enter num: ";
cin >> num;
if(!(num)){
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "num must be an int" << endl;
}else if(num <= 0){
cout << "num must be greater than 0" << endl;
}else if(static_cast<double>(static_cast<int>(num)) != num){
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "num must be an int" << endl;
}else{
break;
};
};
I've been looking through SO and I found some threads that addressed similar issues, but nothing that is specific to what I'm trying to achieve. I'm only trying to accept integer inputs, no decimals, no strings, no characters. If I enter in a negative number or 0
, it'll throw me an error saying "num must be greater than 0." If I enter in a
, it'll throw me an error saying "num must be an int." If I enter 1.0
, it'll throw me an error saying "num must be an int."
The problems I'm running into with this is when I enter in 0
for example, instead of executing the conditional statement that checks (num <= 0)
, it runs the conditional statement that says (!(num))
. The other problem I'm running into is when I enter in a value that has a decimal, like 2.0
, it'll truncate the numbers after the decimal and send in 2
as the value, completely glossing over the check to see if it's a decimal value and telling the program that it's a valid integer when it's not.
Does anyone have a solution for this, or an article link that solves problem like mines? Thank you!