Using Array to Check Equality

This is a discussion on Using Array to Check Equality within the C Programming forums, part of the General Programming Boards category; Hey guys, I'm back again... Need help trying to figure out how to fix this code. It's about checking an ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    116

    Using Array to Check Equality

    Hey guys, I'm back again...

    Need help trying to figure out how to fix this code. It's about checking an array to another array. The problem that I am having is that when I have two address name david and david3, david3 comes up with the info perfectly, but the david address comes up with info but blank. Or even if I have one address named david3, and david name is searched, it'll come up with info but blank.

    Can you help me revise my code to prevent this error?

    It works, but it doesn't work for very very very similar names (ie: david/david3 or kumar/kumar2, etc...)


    Thanks

    Code:
    printf("\n***********Search an address************");
              printf("\nEnter nickname to search: ");
              scanf("%s", search);
    
              j=0;
              for(i=0; i< M-1; i++)
                {
                  while(search[j] == name[i][j] && search[j] != '\0' && name[i][j] != '\0')
                    j++;
    
                  if(search[j] == '\0' && name[i][j] == '\0')
                    {
                      are_equal = true;
                      index_temp = i;
                      break;
                    }
    
                  else if(search[0] == 0 && name[i][0] == 0)
                    continue;
    
                  else
                    are_equal = false;
                }
    
             if(are_equal == false)
                {
                  printf("\nNickname does not exist, address not found");
                  goto end;
                }
    
              if(are_equal = true)
                {
              *****REST OF THE CODE****

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    You have been very stingy on the code you are showing, and us poor souls have to make a lot of assumptions that the parts not shown are done correctly.

    However, 'ol eagle eye here did find this...
    Code:
     if(are_equal = true)
    You're welcome.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    116
    well, I'm sure that you don't want to look through the rest of the code which is over 400 lines long...because I posted it before and not very many people wanted to look at it

    what do you mean by that if statement?

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    That it has to be ==, not =

    Also, I think \0 and 0 are the same. Or are USUALLY the same.

    Now, we have no idea what you do next, so your statements "the info comes blank" has no real search since we see ONLY the comparison.

    EDIT: Check if you are missing a {, maybe at the while? It's a bit confusing
    Last edited by C_ntua; 10-03-2008 at 12:32 PM.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Heh.. I am going to start calling Todd "Eagle eye." Good catch, by the way.

    You have kind of a logical problem as well...

    Code:
                  else if(search[0] == 0 && name[i][0] == 0)
                    continue;
    This code is fine, except for logically you are basically doing your error checking in this fashion

    If string1 has this and string2 has this
    else if string1 has that and string2 has that
    else if string1 exists and string2 exists
    ...

    Wouldn't it make more sense to verify that the strings exist prior to peeking at the info that they may contain? I hate when programs die due to an access violation.

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    116
    well, does that even really matter (checking it prior to peeking at it) when my main concern right now is trying to distinguish david and david3, for example

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    116
    ok nevermind...

    By changing the if statement:

    if(are_equal == true) : from : if(are_equal = true)

    fixed my concern...Thanks!

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    My point is not non-trivial. I only point out non-trivial things when I see code posted by Elysia or tabstop. For you beginner types I usually try to explain why something more serious is a problem. Should I have bolded and underlined access violation? Does that help? It should be the first thing you check.

    Or leave it, I don't care. At least when it just crashes and gives little explanation as to why at least you can nod and say "Oh yeah... that is what he was talking about."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 08:01 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  3. Code: An auto expanding array (or how to use gets() safely).
    By anonytmouse in forum Windows Programming
    Replies: 0
    Last Post: 08-10-2004, 01:13 AM
  4. Using typeid to check if something is an array
    By Trauts in forum C++ Programming
    Replies: 3
    Last Post: 04-30-2003, 08:58 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21