how to stop a string(letter) being entered into an integer? the outcome i want is a message saying "you cant enter that" how do i do that? eg the code i need thanks
no what i mean is my program you have to enter 3 numbers and it will give you an outcome what i want is if someone enters a letter or symbol it to display an error message
If you read into an integer, a letter won't be entered. All you have to do is check the status of the input stream, and dispose of the non-integer input.
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
#include <limits>
usingnamespace std;
int main()
{
int n;
while( cout << "Enter a number, please: "
&& ! (cin >> n))
{
cout << "You can't enter that\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
cout << "Thank you for entering the number " << n << '\n';
}