User input validation for non-duplicate elements into an array

When compiled this code runs and after first entry, it jumps into the for loop as if the first number is a duplicate and then continues to fail from there. I've tried a while loop, with and without bool, and several variations of this for loop. I even tried hard coding it to read if ( user[0] == user[1]....) accounting for every value and it still jumps in as if there is an error. This is part of a much larger lottery program. Removing the input validation part from 'int x,y' down to where the < or > while loop is and it's working fine. I am failing to see, and I'm thinking it's something simple, where the validation isn't working. I've looked at several examples across this site, S.O., YouTube, etc and most examples are for comparing same numbers in two arrays. Several were for printing the array without duplicates. I've read Ch 7 of Tony Gaddis latest C++ book on vectors and arrays as well. I wish there was space to show all the variations of code I've tried, some may have been amusing. Thanks for looking!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void human(int user[], int size) {
	const int SIZEOF = 5;
	cout << "Enter your 5 lottery number picks.\n";
		for (int count = 0; count < SIZEOF; count++)
		{
			cout << "Number " << (count + 1) << ": ";
			cin >> user[count];
			int x, y;
			for (x = 0; x < SIZEOF; x++)
				for (y = x + 1; y < SIZEOF; y++)
				{
					if (user[x] == user[y]) 
					{
						cout << "Please use a unique number: ";
						cin >> user[count];
					}
				}			
			while (user[count] < 0 || user[count] > 9) {
				cout << "You must enter a number between 0 and 9: ";
				cin >> user[count];
			}
		}
}
Unfortunately there isn't a single thing wrong with this code. Everything between lines 8 and 17 is wrong.
I prefer if you figure it out yourself, so this is what I'll do. I'll rewrite this block and I'll leave a space, and I want you to fill that with your own code:
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
bool exists(int array[], int size, int search_term){
	//Write your code here. The code must return true if search_term exists
	//in array[0], array[1], ..., array[size - 1].
}

void human(int user[], int size) {
	const int SIZEOF = 5;
	cout << "Enter your 5 lottery number picks.\n";
		for (int count = 0; count < SIZEOF; count++)
		{
			cout << "Number " << (count + 1) << ": ";
			int input;
			cin >> input;
			if (exists(user, /*What should be passed here?*/, input)){
				cout << "Please use a unique number: ";
				cin >> input;
				//What if input is still not unique? Should this if block be
				//replaced by something else?
			}
			while (input < 0 || input > 9) {
				cout << "You must enter a number between 0 and 9: ";
				cin >> input;
				//What if input is *still* not unique here?
			}
			user[count] = input;
		}
}
I will give it a shot thank you!
I forgot to say, once you've figured it out, come back. I'll explain why your first code didn't work.
Will do! I'm getting there I think. A couple of hours in now on reading new stuff, the Gaddis book did not cover near enough detail on functions so I'm trying to find more intricate information. So many videos and websites stop at the 'here's the structure of a function. Now here's a function with two 'int' values. Here's two numbers to add together, return that value. There, you're a function master now.' No not quite but I will be! I'll continue to work on this, thanks again.
Ok @helios! I came up with something and it's working. I got caught up in your function and have identified I still need to learn more about functions with more than 2 parameters and a simple arithmetic return, which is where a significant amount of sights and videos and my book all end. So I learned up on bool in general and searched up 'bool for input validation for unique numbers in an array'. Sifted through a lot of overly basic concepts and finally found a nugget where the guy discussed how the array boxes worked and how to read through the first one, like I was attempting, and how to read through it again up to the used index at the first for loop with the second loop. I got hung up on a while loop for a while, trying to make it while '=true' or '=false', and I think that's what you were hinting at but I never got there. Using tho the concept of reading one box at a time and then decrementing when a non-unique variable was found I came up with this:

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
// Function to take in human guesses
void human(int user[], int size) {
	const int SIZEOF = 5;
	cout << "Enter your 5 lottery number picks.\n";
	for (int count = 0; count < SIZEOF; count++)
	{
		cout << "Number " << (count + 1) << ": ";
		int input;
		cin >> input;
		while (input < 0 || input > 9) 
		{
			cout << "You must enter a number between 0 and 9: ";
			cin >> input;
		}
		bool isUnique = true;
		for (int j = 0; j < count; j++)
		{
			if (user[j] == input)
				isUnique = false;
		}
		if (isUnique == true)
			user[count] = input;
		else
		{
			cout << "Please enter a unique number: ";
			count--;
		}

	}
}


The logic is essentially the concept I was reading about, he spelled it out pretty clearly, only put into a correct form of syntax, still using the for loop. It still took many tries. Thinking about your comment of 'what if it's still not unique' as it enters the range verification while loop, I thought let's put that in front of the unique checker. This way any number that is too big or too small will already be verified and then it can check for uniqueness having already satisfied the range condition. And lastly as to why my original code wasn't working correctly, I have a guess now after watching the guy bounce numbers in and out of the array. My guess is my code was adding into index 0, asking the second loop to check, it saw it's own 0 + 1 index filled with that element, saw it as equal, and continued doing that through every iteration. At no point was I asking it to empty the container that stored the duplicate. Possibly? I almost have my head wrapped around it all. Super curious about the answer tho!

My next task is to take that code out and make it a function like you originally suggested. I have 2 more spots that will need to use that same validation inside a computer generated lottery array to compare user input against, and a randomly generated 'quick pick' for the human to use instead of picking their own numbers. That's my next task and I THINK I can figure that out. I'm reading more on CPlusPlus website and what not on functions and just need to find more complex examples and logic.
Yeah, that code you have there works. It's not how I personally would have done it, but there's nothing wrong with it.

And lastly as to why my original code wasn't working correctly, I have a guess now after watching the guy bounce numbers in and out of the array. My guess is my code was adding into index 0, asking the second loop to check, it saw it's own 0 + 1 index filled with that element, saw it as equal, and continued doing that through every iteration. At no point was I asking it to empty the container that stored the duplicate. Possibly?
No. It's because your original loop on lines 8-17 was checking if there were any pairs of duplicate numbers in the entire array. But there was no guarantee that the array had been set to anything after 'count'. For example, if human() was called like this:
1
2
int array[5];
human(array, 5);
the contents of array are undefined. Some execution environments initialize uninitialized stack memory to specific values (e.g. Visual Studio in debug mode initializes it to 0xCC). If you were using MSVC, that array would have been filled with -858993460.

By the way "undefined" in C and C++ isn't a distinct value like in JavaScript or other scripting languages. When C and C++ people talk about something being undefined, they're saying that the language gives no guarantees about that thing.
When a value is undefined, reading it will give something unpredictable until you actually run it:
1
2
int this_is_undefined;
std::cout << this_is_undefined;
When a behavior is undefined, it becomes impossible to reason about what the program will do. The language allows a program with undefined behaviors to do anything.
1
2
3
int *dont_write_to_me;
*dont_write_to_me = 42;
//Now anything can happen. Literally anything! 
Wow thank you. That mostly makes sense. I'll need to read it a few times to really take it in. The issue with being mostly self-taught is that every time I think I'm starting to grasp the bigger picture, the picture gets bigger. I've defined for myself at least some areas I'm weak on that need much improvement before moving on. Well thanks again!
Topic archived. No new replies allowed.