The problem I'm trying to solve is checking an array as the user is inputting values into the array. The specific instructions are: to use a separate method to do the checking; the method must sort the array first; the duplicate check has to be performed in real-time, meaning the user won't first enter all values then the array go back and check them. I've looked around and while I've figured out how to sort, I am completely stumped as to how to check for duplicates as each value is entered. Can anyone nudge me in the right direction of thinking?
This is what I came up with, working within the limits of the assignment:
void getLottoPicks(int numbers[])
{
cout << "Please enter your 7 lotto number picks between 1 and 40: " << endl;
for (int i = 0; i < SIZE; i++)
{
cout << "Selection #" << i + 1 << endl;
cin >> numbers[i];
while (numbers[i] < 1 || numbers[i] > 40)
{
cout << "Please choose a number between 1 and 40: " << endl;
cin >> numbers[i];
}
for (int j = 0; j < i; j++)
{
while (numbers[i] == numbers[j])
{
cout << "You have already picked that number. Enter a different one: " << endl;
cin >> numbers[i];
}
}
}
}
It compiles and runs without any bugs (none that I've caught yet, anyway). If anyone has any feedback it'd be appreciated. I know this kind of algorithm is highly inefficient but our arrays are only 7 values long and this is what was asked for. Initially I thought it had to be sorted, but that was only required if we chose to do the check in a different function.
std::sortandstd::unique.std::set... :P Add the values to a set, then copy them to an array once the input's done, if you simply must have an array.