Input Validation for int not working

I am trying to get the function to check if the user input is between 1 and 40. If it is not, it should cout "Invalid number input. please select again.
But whenever I try to enter a number less than 1 or greater than 40 it displays the proper cout statement, but it inputs a 00 into those element slots of the array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  void getLottoPicks(int ticket[], int size)
{
	int num = 0;
	clearTicket(ticket);
	cout << "Choose 7 numbers between 1 and 40 and enter them 1 at a time: ";
	cout << endl;
	for (int x = 0; x < size; x++)
	{
	    cin >> num;
		if (num > 1 && num < 40)
		{
			while (!NoDuplicates(ticket, num, size))
			{
			 cout << "Cannot have the same number twice, please reenter: " << endl;
				cin >> num;

				}
				ticket[x] = num;

			}
			else
			cout << "Invalid Selection, Please choose again." << endl;
		} 
}
Last edited on
after your line 22 you don't give an option to re-write the number...
Hi,
Can you show us your full code?
I figured it out. The error was in my else statement. I was only displaying the message Invalid Selection, rather than displaying the message then asking the user again to input a valid number.

Silly me. :)
Good to hear :)
Topic archived. No new replies allowed.