I try to identify if a number is an interger or not.
When I run this, I enter a number, such as 5.5, it shows "5.5 is not int. Please try again: ". Then I enter the letter, such as 'a', it shows "5.5 is not int. Please try again: ". The letter 'a' is a character, not integer, I think it should go to the second case and must show "No letter please", but it isn't.
When I first enter a letter, such as 'D', the program run "Please no letter" unlimited times. I wants it shows "Please no letter" but only once, then I can enter another number in this loop.
How can I fix these errors?
while (true) {
while ((num) != static_cast<int>(num)) {
cout << "\t" << num << " is not int. Please try again: ";
cin >> num;
cin.clear();
cin.ignore(80, '\n');
}
while (!(cin >> num)) {
cout << "\tNo letter please: ";
cin >> num;
cin.clear();
cin.ignore(80, '\n');
}
cout << "Good! " << num << " is an int!\n\n";
}