Prevent program from crashing when entering a char in an int?

So I have my code, and you have to write a number. But if you enter a character it just shuts down... How can I make it read characters too withour crashing? Nope that the user chooses the number so it can be anything from 1-12.
You can try this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int i;     // you will enter i
int f;    // flag
		do{
			f = 1;
			cout << "\nEnter the value:  ";
			
			if( !(cin >> q) )
			{
				cin.clear();
				cin.ignore( 100, '\n' );
				f = 0;
			}
		}
		while( !f );


Loop will end when you enter number
Topic archived. No new replies allowed.