question

i was wondering how do you put an error message if a user enters letters,
i want the input to only accept numbers
For eg. like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()
{
    int x;
    while (true)
    {
	cout << "Enter integer: ";
	cin >> x;
	if( cin.fail() )       // Look up this one
	{                      //     and 
		cin.clear();   // these two
		cin.ignore();  // functions!
                cout << "You Bastard!\n";
	}
        else
            break;
    }
    cout <<"You entered " << x;
    cin >> x;
}

That's assuming you use cin, but I'm pretty sure you do.
Last edited on
Topic archived. No new replies allowed.