Like Tree3Likes
  • 1 Post By Tclausex
  • 1 Post By workisnotfun
  • 1 Post By Adak

String comparison troubles: mary != mary

This is a discussion on String comparison troubles: mary != mary within the C Programming forums, part of the General Programming Boards category; For some reason my method of comparing string is wrong or there is something wrong with my strings. My output ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    71

    String comparison troubles: mary != mary

    For some reason my method of comparing string is wrong or there is something wrong with my strings. My output looks like this:
    Code:
    parent(bob,mary)You want to check if bob is a parent of mary
    Looking for childnode
    mary == mary
    peter == mary
    john == mary
    and here is the snippet of the relevant code:
    Code:
     
    char *arg1,*arg2;
    char *tmp;
    
    
    strtok(buffer,"(");    
    tmp=strtok(NULL,",");
    arg1 = strdup(tmp);
    tmp=strtok(NULL,")");
    arg2=strdup(tmp);
    parent(hp,arg1,arg2);
    Code:
    int parent(struct family *hp, char* parent, char* offspring){
    
      struct family *cp = hp;
      while(cp->child){
        cp=cp->child;
        printf("%s == %s\n",cp->name,offspring);
        if(strcmp(cp->child->name,offspring) == 0){
          printf("found the child\n");     
          break;
        }
      }
      if(cp->parent1->name == parent)
        return 1;
      else if(cp->parent2)
        if(cp->parent2->name == parent)
          return 1;
      return 0;
    }
    Even after printing out mary == mary, the program does not execute the "found the child" printf and continues traversing the tree. I think the problem is with the strings.
    Last edited by workisnotfun; 02-17-2013 at 07:31 PM.

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Code:
    printf("%s == %s\n",  cp->name           ,offspring);
    if(strcmp(            cp->child->name    ,offspring) == 0)
    
    workisnotfun likes this.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    71
    jesus....thank you =____=
    Crossfire likes this.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    ^^^^ < rofl >
    Crossfire likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. std::string comparison versus int comparison
    By leeor_net in forum C++ Programming
    Replies: 3
    Last Post: 04-12-2009, 07:28 AM
  2. assignment hail mary
    By deimus in forum C Programming
    Replies: 18
    Last Post: 05-11-2007, 10:44 PM
  3. Mary Kate and Ashley Olsen
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 44
    Last Post: 05-03-2004, 03:36 PM
  4. Hmmm... Comparison Troubles...
    By Inquirer in forum C++ Programming
    Replies: 7
    Last Post: 10-10-2002, 08:40 PM

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