Making sure to only enter numbers

I just started a new program and I only want numbers to be inserted, if it's not a number I want it to loop and say invalid.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
	{
		int array [20];
		int num;
		int count;
		
			cout << "How many whole numbers would you like to enter (max 20)?" << endl;
			cin >> num;

			do
			{
				if (num <= 20)
				cout << "Enter " << num << " numbers. " << endl;
			
				else
				cout << "Invalid. Enter another amount." << endl;
				cin >> num;

			}			while (num != false);


	}

Right now if I insert a higher number than 20 it goes to invalid. When I enter a letter it just starts looping negative numbers infinitely without a pause. Any ideas of what I'm doing wrong?
Last edited on
It will loop infinitely even if you input 3. If you move lines 6 and 7 inside the do...while, it will repeat the cycle only until you type 0 or not an integer. You might want to put a sensible condition for exit. Also, you might want to use unsigned int, or put a check not to ask "Enter -3 numbers".
Topic archived. No new replies allowed.