Click to See Complete Forum and Search --> : comparing data


dnoy99
November 11th, 2001, 08:10 PM
Im trying to compare the information of a char in a struct to the information to another char in a different struct.


for(int l=0; l<sizedaily; l++)//the size of pArray
{
for(int u=0; u<size; u++)//size of drivername
{
for(int f=0; f<4; f++)//size of the ID field
{
if(strcmp(&drivername[u].ID[f],&pArray[l]->ID[f])==0)
{
c++;
}
cout<<c<<endl;
}
}
c=0;

}

The c represents how many times the data match up. Im having a hard time trying to figure this out and i would appreciate it if someone could help me out. Im trying to compare the data and see which ones match up. pArray is a pointer too. Thanks

James Curran
November 11th, 2001, 09:38 PM
First of all, if you are going to use these complex structs, and you want help from others, you MUST, MUST, MUST! give the declaration.

Also, you've hit a new peak: &pArray[l]->ID[f]

You are derefencencing that 4 times!

Now, i"m going to assume that both ID[]s are defined as "char ID[4]", which means you don't need the innermost loop at all. strcmp() compare string, not characters. So, assuming the ID is "1234" for each, first you were comparing "1234" to "1234", then "234" to "234", then "34" to "34", and then "4" to "4". Eliminating that loop gives us:for(int l=0; l < sizedaily; l++) //the size of pArray
{
for(int u=0; u<size; u++)//size of drivername
{
if(strcmp(drivername[u].ID, pArray[l]->ID)==0)
{
c++;
}
cout << c<< endl;
}
}
c=0;
}


Note that "ID" is shorthand for "&ID[0]", so when we got rid of the loop, we could get rid of the rest as well.

However, as I recall, ID is exactly four characters, with no terminating zero, so you can't use "strcmp". NO problem, memcmp() take a length: if(memcmp(drivername[u].ID, pArray[l]->ID, sizeof(pArray[0]->ID) )==0)


You could use "4" instead of "sizeof(pArray[0]->ID)", but don't. sizeof will continue to work even if you change the size of ID.








Truth,
James
http://www.NJTheater.com
http://www.NovelTheory.com
I don't do it for the points (OK, maybe I do), but rating a post is a good way for me to know if I helped.