# Thread: searching array for matches

1. ## searching array for matches

hey all, i'm having troube searching this array to find identical matches. i though i had the logic figured out, but it's not working. thanks for your help.

Code:
```#include <iostream.h>

#define draw 5

void f_pair (const char *face_card[]);

main()
{
const char *face_card [5]={"1", "2", "3", "1", "5"};
f_pair(face_card);
return (0);
}
void f_pair (const char *face_card[])
{	int j=1;
for (int i=0; i<draw-1; i++)
{
for (j+=i; j<draw;++j)
{	 if (face_card[i]==face_card[j])
{	cout<<"you matched: "<<face_card[i]<<endl<<endl;
}else
cout<<"no match"<<endl;
}
}

}```

2. ## a couple problems.

for (j+=i; j<draw;++j)

Will not work. The first time through the loop, it will work, yes, because j=1, so adding i would give you i+1. But, the second time through the loop, j=draw. So j+=i would give you draw+i.

for (j=i+1; j<draw;++j)

Secondly, this will not work as intented:

if (face_card[i]==face_card[j])

What you are doing are comparing locations in memory of the strings, instead of the actual strings. For instance:

int b[3] = {1, 2, 3};
int a[3] = {1, 2, 3};

if(a == b) cout << "same" << endl; else cout << "not same" << endl;

The arrays have the same elements, but they are in different locations in memory.

Since the strings are all one character long, comparing the first character of each string would work:

if (*face_card[i]==*face_card[j])

Or, what would probably be prefered, would be to use strcmp, in case you wanted to extend the strings to hold more than one character:

if (strcmp(face_card[i], face_card[j])==0)

You'll need to include <string.h>

Why are you using strings anyway? Why not an integer array?

Finally, I'm not quite sure what you are attempting with:

cout << "no match" << endl;

Do you want to check to see if there are no matches at all in the array? Or for each number, see if there are any matches to that number or not?

To do the first one (no matches at all in the array), you would want to keep track of whether or not:

cout<<"you matched: "<<face_card[i]<<endl<<endl;

was printed. If it isn't printed at all, then display "no match". Use a variable to do this:

if (strcmp(face_card[i], face_card[j])==0)
{
cout << "you matched: " << face_card[i] << endl;
matches=1;
}

You would of course want to initalize matches to 0 in the variable declaration. And then, at the end of your function, you can do:

if(matches==0) cout << "No match" << endl;

I hope this helps.

3. hey, thanks a lot, that really hit the spot.