Help with strncmp fxn

This is a discussion on Help with strncmp fxn within the C++ Programming forums, part of the General Programming Boards category; Hey guys I can get this to compile, but the results should come back as 0 or 1 but if ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    29

    Help with strncmp fxn

    Hey guys I can get this to compile, but the results should come back as 0 or 1 but if i try to output the results of c1 or c2, I get -44 or 8, which does not make sense. What might be wrong with this statement:

    Code:
      for( int i=0; i<36; i++){
        int c1=0;
        int c2=0;
      //compares user locations with the input file
       c1= strncmp(userLocA, b[index].getLocationOne(),strlen(userLocA));
       c2 = strncmp(userLocB, b[index].getLocationTwo(),strlen(userLocB));
      
       if(c1==0 && c2==0)
          
        
          break;
      }

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Declaration:

    int strncmp(const char *str1, const char *str2, size_t n);

    Compares at most the first n bytes of str1 and str2. Stops comparing after the null character.

    Returns zero if the first n bytes (or null terminated length) of str1 and str2 are equal. Returns less than zero or greater than zero if str1 is less than or greater than str2 respectively.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > c1= strncmp(userLocA, b[index].getLocationOne(),strlen(userLocA));

    Maybe this should be i?
    c1= strncmp(userLocA, b[i].getLocationOne(),strlen(userLocA));

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    29

    Arrow The Dog

    The Dog You have to excuse me I am a little new at this can you help me apply this to my function?


    Swoopy, no I have index from earlier in my program its a string I need to compare from an input file stored in b which is a class i created

  5. #5
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    >> Hey guys I can get this to compile, but the results should come back as 0 or 1

    The return value of strncmp() could be a negative number ( < 0 ), or 0, or a positive number ( > 0 ).

  6. #6
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    >> Hey guys I can get this to compile, but the results should come back as 0 or 1

    The return value of strncmp() could be a negative number ( < 0 ), or 0, or a positive number ( > 0 ).

    Therefore "-44" shows that the string is less than, and "8" shows that the string is more than.

    Print the string that you are comparing before you compare them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about strncmp
    By sweetorangepie in forum C Programming
    Replies: 8
    Last Post: 03-24-2008, 01:15 PM
  2. unsure of how to use the functions strcmp and strncmp
    By hellgrammite in forum C Programming
    Replies: 8
    Last Post: 12-06-2007, 12:47 AM
  3. strncmp like function in C#
    By x77 in forum C# Programming
    Replies: 4
    Last Post: 11-18-2007, 05:29 AM
  4. Problem passing a ptr to a fxn into a fxn
    By *Bob_ptr in forum C Programming
    Replies: 2
    Last Post: 04-07-2004, 12:52 PM
  5. Prime Factor Fxn
    By alpha in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2003, 10:44 AM

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