using isDuplicate in a function

I think its silly, but curious how and where to put the bool isDuplicate Function that I have created in my void getChoices(). I'm so close to complete my entire program.


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
bool isDuplicate( int array[], int arraySize, int searchNum )
{
    
    //Loop through the array to determine if it is holding searchNum
    //If a match is found, return true
    
    for( int num = 0; num < arraySize; num++ )
    {
        if( array[num] == searchNum )
            return true;
    }
    
    //At this point, no match was found in the array, so false is
    //returned.
    
    return false;
}



void getChoices(int userArray[], int MAX_USE_NUM)
{
    int userNum = 0;
    
    for (int numValues = 0; numValues < MAX_USE_NUM ; numValues++)
    {
        
        cout<<"User choice " << numValues+1 << ": ";
        cin>> userNum;
        
       while (userNum<0 || userNum>10)
        {
            cout<<"Choice must be between 1 and 10, reenter: ";
            cin>> userNum;
            
            if(isDuplicate(Array, userNum, num))
            {
                cout << " Already been used reenter a different number: ";
                cin >> userNum;
            }
        }
        
        userArray[numValues] = userNum;
   
    }
    
}
You have two nested loops. Would it be enough to have just one?
1. Read number
2. If number is valid, then add it to list
3. Repeat from 1 until the list is full

Your use of the isDuplicate. Compare the purpose of each function argument to the purpose of each variable that you use when you call the function. Do you notice a logical mismatch?


Overall logic. How large can the MAX_USE_NUM be? You only allow 11 unique values (which contradicts the instruction you do give to the user on line 33). If you do in practice use all allowed values, then the only thing up to the user is their order.

Would it be good to show the list of values that the user can choose from at each stage, rather than forcing the user to remember that 7 is the only valid number that they have not typed yet?
symbolic constant: MAX_USE_NUM =5

i simply have that one loop to detect if its between 1-10
if not re-enter again

the isDuplicate should help with avoiding repeated number

like
choice 1: 1
choice 2: 3
choice 3: 5
choice 4: 1 " ERROR! that number already been used pick a different number "
choice 4: 7
choice 5: 9
Topic archived. No new replies allowed.