CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: comparing data

  1. #1
    Join Date
    Nov 2001
    Posts
    28

    comparing data

    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



  2. #2
    Join Date
    Sep 1999
    Location
    NJ
    Posts
    1,299

    Re: comparing data

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured