check for duplicates in array

Hi. I'm trying to check for duplicates and prevent them from being added in my array. I wrote a loop in the addin function to check if a name is a duplicate and display a message asking you re-enter. When i run the code though each time a name is entered the message is displayed whether its a duplicate or not. Any suggestion would be appreciated

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
[code] #include "stdafx.h"
  #include <iostream>
  #include <stdio.h>
  using namespace std;

  const int datamax = 10;
  void addin(char table[5][2][40]);

 int main()
 {
 char tb[5][2][40];
 char response;
 int i = 0;
 cout << "What to do" << endl
      << "A to Add" << endl
    << "Q to Quit" << endl;
cin >> response;
cin.clear();
cin.ignore();

for (i = 0; i < 10 && (response != 'q' || response != 'Q'); i++)
{
    if (response == 'a' || response == 'A')
    {
        addin(tb);
    }
    else
    {
        return 0;
    }
    cout 
        << "What to do" << endl
        << "A to Add" << endl
        << "Q to Quit" << endl;
    cin >> response;
    cin.clear();
    cin.ignore();
}
return 0;
 }
void addin(char addin[5][2][40])
{
static int i = 0;
bool answer = true;
int j = i + 1;
cout << "Please enter name" << endl;
gets_s(addin[i][0]);
if (addin[i][0] == addin[j][0]) {
    answer = true;
}
if (answer == true)
{
    cout << "Please re-enter name " << i + 1 << ". Duplicate names are not allowed:" << endl;
    cin >> addin[i][0];
    cout << "please enter number" << endl;
    cin >> addin[1][1];     
}
answer = false;
i++;
}

.
Last edited on
@SakurasouBusters [edit]removed[/edit]

@Kwaku42
Please pick a language: C++ or C. (I recommend you get rid of all the <stdio.h> stuff and stick with C++ I/O. It's loads easier.)

You are always getting the message because on line 44 you initialize answer to true. Lines 48-50 don't change that. Instead, initialize with false -- this is the default value if lines 48-50 find no duplicate.

Hope this helps.
Last edited on
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/198259/
When i change it false, no message is displayed at all even when the value is a duplicate.
Thanks you for the reply.
closed account (48T7M4Gy)
c-style strings are compared with strcmp
closed account (48T7M4Gy)
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
#include <iostream>
#include <cstring>

using namespace std;

void check(char[3][10], char[10]);

int main()
{
    char name[3][10];
    char trial[10];
    
    strcpy(name[0], "Helen");
    std::strcpy(name[1], "Fred");
    strcpy(name[2], "Margaret");
    
    cout << "Please enter a name: ";
    cin >> trial;
    
    for(int i = 0; i < 3; i++)
    {
        cout << name[i] << '=' << trial;
        
        if(strcmp(name[i], trial))
            cout <<" match not found yet\n";
        else
            cout << " match found <--\n";
    }
    
    //ANOTHER WAY
    check(name, trial);
    
    return 0;
}

void check( char list[3][10], char try_me[10])
{
    for(int i = 0; i < 3; i++)
    {
        cout << list[i] << '=' << try_me;
        
        if(strcmp(list[i], try_me))
            cout <<" Match not found yet\n";
        else
            cout << " Match found <--\n";
    }
    
    return;
}

Please enter a name: Helen
Helen=Helen match found <--
Fred=Helen match not found yet
Margaret=Helen match not found yet
Helen=Helen Match found <--
Fred=Helen Match not found yet
Margaret=Helen Match not found yet
Program ended with exit code: 0

A bit more detail.
Last edited on
Topic archived. No new replies allowed.