Cannot print out same name twice from an array

My project is like this: I decide a number n, and that's the number of names I have to put in an array. I have to write it with the first letter uppercase and the rest is lowercase (example: John, Sara, Peter, Sara, Sara) and the outcome in the end should have:

1) The same name cannot be printed out twice <-- Need help with this part
2) The names have to be in alphabetical order. <-- Not started yet

I have not started working on the alphabet, but I'm working on the Compare function and I think I'm pretty close.. But what do I need to make it 100% right?

Here is my 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <string>   
#include <fstream>
#include <cctype>    

using namespace std;

const int MAXNUM = 100;

void WriteIntoArray (string arr[], int n);
void Compare (string arr[], int n);
void PrintArray (string arr[], int n);

int main()
{
    cout << "Write a number, and then names: ";
    int n = 0;
    string arr[MAXNUM], a;
    cin >> n;

    WriteIntoArray (arr, n);  
    Compare (arr, n);
    PrintArray (arr, n);


    return 0;
}

void WriteIntoArray (string arr[], int n)
{

    for (int i = 0; i < n; i++)
    {
        cin >> arr[i];
    }
}


void Compare (string arr[], int n)
{
    string a, b;

    for(int i = 0; i < n; i++)
    {
        a = arr[i];

        for(int j = 0; j < n; j++)
        {
            if (a != arr[j])
            {
                a = a + arr[j] + " ";
            }
        }
    }
    cout << endl << endl << "This part is not 100% right: " << a << endl << endl;
}

void PrintArray (string arr[], int n)  // The outcome from the function Compare( ) must be
                                                    // done right first to make this function print out right values.
{
    cout << "The outcome from PrintArray function: " << endl;

    for(int i = 0; i < n; i++)
    {
       cout << arr[i] << endl;
    }
}
Last edited on
You're right - you are pretty close. In your compare function, you may want to consider starting the inner j loop at the current value of i+1

Also your if statement does not want to compare arr[j] with the concatenated string a - rather the comparison should be between arr[i] and arr[j]

Finally, I do not believe that you want to concatenate arr[j] onto the end of a inside the inner loop. Instead, consider initialising a bool at the beginning of the outer i loop, for example:

1
2
3
for(int i = 0; i < n; i++)
{
    bool bFoundDuplicate = false;

Then in your inner j loop, you want an if statement that will set this bool to true if you have found a duplicate.

Just before the outer i loop finishes, only append arr[i] onto a if no duplicates have been found, i.e.

1
2
3
4
    } // End of j loop
    if( ! bFoundDuplicate )
        a = a + arr[i] + " ";
}  // End of i loop 

And of course you'll need to get the a string out of this function, perhaps by setting the function's return type to string and returning a at the end.

Hope that makes sense!
Thank you very much for you time Norm Gunderson! I've taken your advice but it actually did not do any better. Is there something I misunderstood and did not do right in this updated code?

void Compare (string arr[], int n)
{

bool bFoundDuplicate = false;

string a;
for(int i = 0; i < n; i++)
{
bFoundDuplicate = false;

for(int j = i+1; j < n; j++)
{
bFoundDuplicate = true;
}
if( !bFoundDuplicate )
a = a + arr[i] + " ";
}
cout << endl << endl << "This part is not 100% right: " << a << endl << endl;
}
Almost there - you're just missing the if statement to only set bFoundDuplicate when you find a duplicate. I've added some debug output to the code below to make it easier to understand the code behaviour when you run your 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
void Compare (string arr[], int n)
{
    bool bFoundDuplicate = false;

    string a;
    for(int i = 0; i < n; i++)
    {
        bFoundDuplicate = false;
        // Debug output
        cout << "Comparing \"" << arr[i] << "\" with:" << endl;

        for(int j = i+1; j < n; j++)
        {
            // Debug output
            cout << "\t\"" << arr[j] << "\"";

            if( arr[i] == arr[j] )   // This IF statement is the only meaningful change I made
            {
                bFoundDuplicate = true;
                // Debug output
                cout << " - Duplicate Found!";
            }
            // Debug output
            cout << endl;
        }

        if( !bFoundDuplicate )
            a = a + arr[i] + " ";
    }
    cout << endl << endl << "This part is 100% right :) :" << a << endl << endl;
}

Great man! Thanks :)
Topic archived. No new replies allowed.