Shortening while loops

I've been scratching my head over this problem for a while now, I'm not sure how to shorten up these while loops into a few for loops and still get the job done. What i have to do is have someone enter 4 numbers and at the same time validate that those 4 numbers are between 1-100 and don't repeat. I have a version of one with while loops and one with for loops thats incomplete (and probably wrong).

***WHILE LOOP VERSION***

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
cout << "\n\n" 
		<< "Enter a number between 1 - 100." << endl;
	cin >> one;

	while(size < 1 || size > 100){
		cout << "\n Enter a different number between 1 - 100.  ";
		cin >> one;
	}//end while
	
	cout << "\n\n"
		<< "2: Enter a number between 1 - 100." << endl;
	cin >> two;

	while(two == one || two < 1 || two > 100)}
		cout << "\n Enter a different number between 1 - 100.  ";
		cin >> two;
	}//end while

	cout << "\n\n" 
		<< "3: Enter a number between 1 - 100." << endl;
	cin >> three;

	while(three == one || three == two || three < 1 || three > 100){
		cout << "\n Enter a different number between 1 - 100.  ";
		cin >> three;
	}//end while

	cout << "\n\n" 
		<< "4: Enter a number between 1 - 100." << endl;
	cin >> four;

	while(four == one || four == two || four == three || four < 1 || four > 100 ){
		cout << "\n Enter a different number between 1 - 100.  ";
		cin >> four;
	}//end while 


***FOR LOOP VERSION***

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int size = 0;

	for(int i = 1; i <= 4; i++){
		cout << "\n\n" << i
		<< ": Enter a number between 1 - 100." << endl;
		cin >> array1[size];
		

		for(int i = 0; i <= 4; i++){
		
		while(array1[size] < 1 || array1[size] > 100){
		cout << "\n Enter a different number between 1 - 100.  ";
		cin >> array1[size];
		i++;
		}//end while 

size++;

'what do i need to add so that the for loops are complete??
(And i'm as nooby as it gets so please explain why also)
The loops are working fine it is your body that I believe is the problem. Each increment of your for loop is reading into the same element of the array. Instead of using array[size] try array[i], should work then.
yes, there was no problems in compiling the while loop code, but i want to use arrays and for loops in my program. Dont know how to validate the previous elements in the array so none are the same.
I was also talking about the for loops. In those loops you have written the number the user enters is being saved into the same element of the array each time because the element isn't a variable in your code it is a constant.

Instead try:
1
2
3
4
5
6
7
8
9
10
11
12
13
for(int i = 0; i <= 4; i++)
{
        cout << "Enter a number between 1 - 100. ";
        cin >> array[i];

        for(int j = 0; j <= i; j++)
        {
                if(array[j] == array[i] || array[i] < 1 || array[i] > 100)
                {
                      cout << "Enter a different number 1 - 100. ";
                      cin >> array[i];
        }
}

This code should prompt the user for 5 different numbers in the range of 1 - 100 and check each entered number against the other numbers in the array to make sure there are no duplicates.
Last edited on
Consider using a function, say int readNumber(int min, int max, int * values, int valuesSize), so you don't need to copy-paste your code (always a good practice). The function prompts the user for a number until it's between min and max and is not contained in values. Also, you should use do while instead of while in these situations.
You seem to do that (switching index i with counter size) often in both versions of the code. Do mind, your "for loop version" had the inner while using the outer for's 'i' as counter as well. That's bad.
I'm still confused,
@Yellowhottub i dont think the code worked, but I want to figure it out myself so please give me hints ^^
@bbgst, i want to use arrays and I dont see how I can use a do while in this situation.
@gaminic, thanks for the tip
I got it!! i'll use a linear search :P
Topic archived. No new replies allowed.