sting/integer

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
I'd like to know more information.
Your question : your string input is numbers only? (means Number format - 0123456789)
Last edited on
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>
using namespace 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';
}
that great thanks
Topic archived. No new replies allowed.