Sorting string array

This is a discussion on Sorting string array within the C++ Programming forums, part of the General Programming Boards category; How is Unique declared? -- Mats...

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    How is Unique declared?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #17
    Registered User
    Join Date
    Dec 2008
    Posts
    66
    Code:
    string Unique [BOOK_MAX];

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, can you post the whole function that this is in?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #19
    Registered User
    Join Date
    Dec 2008
    Posts
    66
    Uhh, all my code is int main. I'm still not very good with functions and due to deadlines I decided to just do it the way I can. Here's the whole case, if that helps any:
    Code:
       int select;
       cin >> select;
    
       switch (select)
         {
         case 1:
           {
             int flag = 0;
             int count = 0;
             for (int i = 0; i < (BOOK_MAX); ++i)
               {
                 if (Books[i].Author == "")
                   {
                   }
                 else
                   {
                         Sort[count] = Books[i].Author;
                         ++count;
                   }
               }
            sort(Sort, Sort+count);
    
            int uniquecount = 0;
            for (int x = 0; x < count; ++x)
              {
                if (Sort[x] == Sort[x+1])
                  {
                  }
                else
                  {
                    Unique[uniquecount] = Sort[x];
                    ++uniquecount;
                  }
              }
    
            /*for (int q = 0; q < uniquecount; ++q)
              {
                for (int w = 0; w < BOOK_MAX; ++w)
                  {
                    if (Unique[q] == Books[w].Author)
                      {
                        cout << Books[w].Title << " in section " << Books[w].Category;
    
                        for (int e = 0; e < BOOK_MAX; ++e)
                          {
                            if (Book[w].ID == Loans[e].Book)
                              {
                                cout << " - on Loan." << endl;
                              }
                            else
                              {
                                cout << " - in Stock." << endl;
                              }
                          }
                      }
                  }
              }
            */
    
            cout << Unique[1] << Unique[2] << endl;
            break;
           }

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And about 5 lines around the declaration of Unique...

    --
    mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #21
    Registered User
    Join Date
    Dec 2008
    Posts
    66
    Code:
    Book Books [BOOK_MAX];
    User Users [USER_MAX];
    Loan Loans [BOOK_MAX];
    string Sort [BOOK_MAX];
    string Unique [BOOK_MAX];
    
    int main()

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,302
    Quote Originally Posted by Furious5k
    Uhh, all my code is int main. I'm still not very good with functions and due to deadlines I decided to just do it the way I can.
    I have a feeling that this is a case of "more haste less speed".

    You could start by turning the block of code following case 1 into a function. Give it a descriptive name, add a comment that describes what the function is supposed to do. It looks like you made the mistake of using global variables unnecessarily, but nevermind, for the function practice using arguments. You would find that when you focus on a small piece of code without distractions (thanks to local scope), reasoning about your program and figuring out what is wrong becomes easier.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #23
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    If you're allowed to use the standard library for sorting, perhaps you're also allowed to use it for removing duplicates as well?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #24
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
                            if (Book[w].ID == Loans[e].Book)
    see anything wrong? If not, look closer!

    Code:
                if (Sort[x] == Sort[x+1])
                  {
                  }
                else
                  {
                    Unique[uniquecount] = Sort[x];
                    ++uniquecount;
                  }
    How about using the != operator instead of == operator?

    And x+1 still goes outside of the array.

    Yes, I agree with Laserlight - your case 1: should definitely be it's own function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #25
    Registered User
    Join Date
    Dec 2008
    Posts
    65
    Why don't you just overload operator< for your Books Class and call sort() directly on your class.

    Code:
    bool Books::operator<(const Books obj) const
    {
        if(Author < obj.Author)
            return true;
        else if (Author == obj.Author)
            return true;
        else return false;
    }
    Then just delete the empty arrays. Of course I am usually wrong.

  11. #26
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Phyxashun View Post
    Why don't you just overload operator< for your Books Class and call sort() directly on your class.

    Code:
    bool Books::operator<(const Books obj) const
    {
        if(Author < obj.Author)
            return true;
        else if (Author == obj.Author)
            return true;
        else return false;
    }
    Then just delete the empty arrays. Of course I am usually wrong.
    That's not a bad idea - but surely operator< should not return true for Author == obj.Author? [and const Books& obj, perhaps?]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #27
    Registered User
    Join Date
    Dec 2008
    Posts
    65
    Quote Originally Posted by matsp View Post
    That's not a bad idea - but surely operator< should not return true for Author == obj.Author? [and const Books& obj, perhaps?]

    --
    Mats
    I agree with the passing by reference, but if you have books of the same author wouldn't they be equal? i'm not too sure.

    EDIT:

    Well wouldn't you want them to be treated equal by the sort() algorithm, you can also add nested ifs to the overload function to include the other members of the class so that the sort() would be more specific than just sorting the authors. Just me pondering.
    Last edited by Phyxashun; 01-06-2009 at 01:51 PM.

  13. #28
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,302
    Quote Originally Posted by Phyxashun
    but if you have books of the same author wouldn't they be equal?
    If they are "equal", then one is not "less than" the other, so you should return false.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #29
    Registered User
    Join Date
    Dec 2008
    Posts
    65
    Quote Originally Posted by laserlight View Post
    If they are "equal", then one is not "less than" the other, so you should return false.
    Ah, more failed logic on my part. I agree

    EDIT:
    And if you wanted to move the empty class members to the end couldn't you change the overloaded operator< to make it >

    Code:
    bool Books::operator<(const Book &obj) const
    {
        if (Author > obj.Author)
            return true;
        return false;
    }
    Last edited by Phyxashun; 01-06-2009 at 01:58 PM.

  15. #30
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,302
    Quote Originally Posted by Phyxashun
    And if you wanted to move the empty class member to the end couldn't change to overloaded operator< to make it >
    That would result in a reverse ordered sort. More accurately:
    Code:
    bool Books::operator<(const Book &obj) const
    {
        if (Author.empty())
        {
            return !obj.Author.empty();
        }
        else if (obj.Author.empty())
        {
            return false;
        }
        return Author < obj.Author;
    }
    Last edited by laserlight; 01-06-2009 at 02:10 PM. Reason: Fix for the case where the RHS author is empty.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting 2D string array
    By sureshhewa in forum C Programming
    Replies: 14
    Last Post: 07-27-2008, 02:30 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. can't assign proper values to an array of string
    By Duo in forum C Programming
    Replies: 1
    Last Post: 04-04-2005, 07:30 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM

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