simple array

I'm trying to do input validation with this array. I do not want the user to enter a negative number. However, with the way I wrote it, it will compile and when user enters a negative number it will give a debug error. and still count the negative number. What is a better way to do the input validation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
 #include <iostream>


using namespace std;



int main()
{
	int numbers[6];
	int smallest = 0, largest = 0;
	int temp = 0;

	for (int i = 0; i < 6; i++)
	{
		cout << "Enter number" << i + 1 << ":" << endl;
		cin >> numbers[i];

		if (numbers[i] < 0) //input validation does not work as intend
		{
			cout << "Enter only positve numbers. \n";
			cin >> numbers[6];
		}

	}
	smallest = numbers[0];
	largest = numbers[0];

	for (int i = 1; i < 6; i++)

	{
		temp = numbers[i];
		if (temp < smallest)
		{
			smallest = temp;
		}

		if (temp > largest)
		{
			largest = temp;
		}

	}

	cout << "Largest number is: " << largest << endl;
	cout << "Smallest number is: " << smallest << endl;

	return 0;
}

Take a close look at line 22 above. And that if should be a while.
Last edited on
aw. thank you. sometimes its the smallest things.
Topic archived. No new replies allowed.