Like Tree1Likes

strcmp problem

This is a discussion on strcmp problem within the C++ Programming forums, part of the General Programming Boards category; Hey all. I am having a slight problem with strcmp. I am comparing a two last names and two first ...

  1. #1
    Registered User XNOViiCE's Avatar
    Join Date
    Nov 2012
    Location
    'merica
    Posts
    38

    strcmp problem

    Hey all. I am having a slight problem with strcmp. I am comparing a two last names and two first names and the problem occurs when two of the same last names are entered. How can I fix this problem?
    Any suggestions would be great!! NOTE: I am not asking for code just a push in the right direction.

    Code:
     bool LessThanByName( const Passenger & passenger ) const
       {
          char last0[MAX_LENGTH + 1];
          char last1[MAX_LENGTH + 1];
          char first0[MAX_LENGTH + 1];
          char first1[MAX_LENGTH + 1];
          for(int i = 0; i < last[i]; i++)
          {
             last0[i] = toupper(last[i]);
          }   
          for(int i = 0; i < passenger.last[i]; i++)
          { 
             last1[i] = toupper(passenger.last[i]);
          }
          for(int i = 0; i < first[i]; i++)
          {   
             first0[i] = toupper(first[i]);
          }
          for(int i = 0; i < passenger.first[i]; i++)
          {   
             first1[i] = toupper(passenger.first[i]);
          }
          if(strcmp(last0, last1) == 0)
             return (strcmp(first0, first1) < 0);
          return (strcmp(last0, last1) < 0);
       }
    Last edited by XNOViiCE; 12-13-2012 at 08:54 AM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    2,144
    don't use strcmp or char arrays. use std::string.
    Elysia likes this.

  3. #3
    Registered User XNOViiCE's Avatar
    Join Date
    Nov 2012
    Location
    'merica
    Posts
    38
    No I have to use a char array. It is required for it to be used.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Check test condition of for loops.
    If that doesn't solve post how you're getting the input.

  5. #5
    Registered User XNOViiCE's Avatar
    Join Date
    Nov 2012
    Location
    'merica
    Posts
    38
    Quote Originally Posted by DeadPlanet View Post
    Check test condition of for loops.
    If that doesn't solve post how you're getting the input.
    I will but I am fixing things and I made a bigger problem. :b But I will do that soon!

  6. #6
    Registered User XNOViiCE's Avatar
    Join Date
    Nov 2012
    Location
    'merica
    Posts
    38
    Okay so the code has changed a bit now

    Code:
    bool LessThanByName( const Passenger & passenger ) const
       {
          char last0[MAX_LENGTH + 1];
          char last1[MAX_LENGTH + 1];
          char first0[MAX_LENGTH + 1];
          char first1[MAX_LENGTH + 1];
          
          for(int i = 0; i < last[i]; i++)
          {
             last0[i] = last[i];
         }   
          for(int i = 0; i < passenger.last[i]; i++)
          { 
            last1[i] = passenger.last[i];
          }
          for(int i = 0; i < first[i]; i++)
          {   
             first0[i] = toupper(first[i]);
          }
          for(int i = 0; i < passenger.first[i]; i++)
          {   
             first1[i] = toupper(passenger.first[i]);
          }
          
          if(strcmp(last0, last1) == 0)
            {
             return (strcmp(first0, first1) < 0);
            }
          return (strcmp(last0, last1) < 0);
       }
    This is for whoever is wondering where the input comes from
    Code:
     
       void SortByName()
       {
          for(int i = 0; i < passengerCount; i++)
             for(int j = i + 1; j < passengerCount; j++)
             {
                if(list[i].LessThanByName(list[j]))
                   Swap( i, j );
             }
    Why does it crash when the last name is the same?

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,650
    Well it is not a problem of strcmp.

    Check the first and last of the passenger. Check the passengerCount. Some counter may have not the value you think it has.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    Code:
    for(int i = 0; i < last[i]; i++)
    {
        last0[i] = toupper(last[i]);
    }  
    for(int i = 0; i < passenger.last[i]; i++)
    {
        last1[i] = toupper(passenger.last[i]);
    }
    for(int i = 0; i < first[i]; i++)
    {  
       first0[i] = toupper(first[i]);
    }
    for(int i = 0; i < passenger.first[i]; i++)
    {  
       first1[i] = toupper(passenger.first[i]);
    }
    None of those tests look correct? Maybe you meant to use strlen there on the items in question? As a test, you can try to print out last0/1, first0/1 just before the strcmp tests and see if they meet your expectations.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Registered User XNOViiCE's Avatar
    Join Date
    Nov 2012
    Location
    'merica
    Posts
    38
    Hmmm. Okay I am going to check that.

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    2,144
    your for loops make no sense.

    can you please explain exactly why you are comparing the counter to the value stored in the array at that index?

  11. #11
    Registered User XNOViiCE's Avatar
    Join Date
    Nov 2012
    Location
    'merica
    Posts
    38
    I checked the names, the passenger count and I am ment to use strcmp. We are sorting by alphabetical order.
    So the results I got were that the compiler only crashes when the names are the same no matter where they occur in the file.
    The error is also saying I have an array that isn't initialized. Any suggestions?

  12. #12
    Registered User XNOViiCE's Avatar
    Join Date
    Nov 2012
    Location
    'merica
    Posts
    38
    Oh no the for loops are assigning values to last0/1 , first0/1. The reason I am using the stored value is so that it only puts in the number of characters in the array and no more than that.

  13. #13
    Registered User
    Join Date
    Oct 2006
    Posts
    2,144
    you could start by not copying the arrays. there is absolutely no need to do that.

  14. #14
    Registered User XNOViiCE's Avatar
    Join Date
    Nov 2012
    Location
    'merica
    Posts
    38
    Its was a move that my partner wanted. Thinking about it now, I'll clean it up.

  15. #15
    Registered User XNOViiCE's Avatar
    Join Date
    Nov 2012
    Location
    'merica
    Posts
    38
    Elkvis....you sir are a wizard. That some how made it work!
    Also thank you to everyone that helped out! It helps me a lot!

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is this a problem with strcmp?
    By daynorb in forum C Programming
    Replies: 6
    Last Post: 11-29-2011, 08:59 AM
  2. problem with strcmp
    By byebyebyezzz in forum C++ Programming
    Replies: 8
    Last Post: 06-27-2011, 10:20 PM
  3. strcmp problem in c
    By Prodiga1 in forum C Programming
    Replies: 2
    Last Post: 11-22-2010, 11:13 PM
  4. strcmp problem, whats the problem, i cant figure it out!
    By AvaGodess in forum C Programming
    Replies: 14
    Last Post: 10-18-2008, 06:45 PM
  5. Problem with strcmp()....
    By LightsOut06 in forum C Programming
    Replies: 3
    Last Post: 09-02-2005, 12:30 PM

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