Problem with a while loop

I am writing a program that is suppose to fill up an array with numbers between 10 and 100 inclusive but it should not allow repeat numbers.

I wrote the following while loop and it works allowing only numbers between 10 and 100:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
		while (n < arraySize) 
		{
			
			cin >> value; 
			
			if((value >= 10) && (value <=100))
			{
				myArray[n] = value;
				n++;
			}

			else 
			{

				cout << "Wrong value entered." << endl;
				
			}

		}




Now, I would like to add a condition that would eliminate the repeat values.
I tried adding the condition to the if statement with the following line:

if((myArray[n]!= value) && (value >= 10) && (value <=100))

However, for some reason the first condition testing for the duplicate value does not work. If the array is empty, then the condition is true. If the value in the array is the same as value varialble, then the condition is set to false.

Any sugestions on how to work this around?
However, for some reason the first condition testing for the duplicate value does not work

You're checking whether a value in the array that hasn't even been set yet is the same as the value entered.
How do you expect that to work?
Athar,

at the first iteration the array is empty so the condition is true; the value is not in and it is added.
The second iteration should then test to see if the value is already in.

Shouldn't work after the first value is added to the array?
Last edited on
at the first iteration the array is empty so the condition is true;

No, it's true when the first element in the array is different from the one that was entered.

Shouldn't work after the first value is added to the array?

No? You're checking only the element in the array you're about to reset, so basically one that isn't yet part of the valid array section.
Athar,

what would you recommend on doing?

I need to create an array and fill it without duplicating the values.
What you need to do is to check all the values in the array before n (i.e. from 0 to n-1) to see if one matches the one that was entered (using a nested loop).
The simple solution would be to have a bool array that indexes from 10 to 100.

1. Initialize the bool array to all false
2. When you get a new int value, look in the bool array to see whether that number has been entered already.
3. If it hasn't, set that bool element to true, and insert the number into your array
4. If it has, give an error message to the user and prompt for another number.

An alternative approach would be to examine the int array every time you're ready to enter another value. This would result in less memory usage, but at the expense of more processor time. You'll have to decide which is more valuable to you.
Thanks to both of you.

Mzimmeers,

I like your second idea. I will work on it and see if I can come up with and check with the forum tomorrow.
OK...just be prepared that, if your array becomes large, your program may become relatively slow.

But go ahead and experiment, and let us know what you find.
Topic archived. No new replies allowed.