1

When trying to bubble sort an inventory stored in a struct array, I am getting two different errors when I compile the code below:

void SORT_INVENTORY(Books* list, int max, int position)
{
        bool swap;
        string temp;

        do
        {
                swap = false;
                for (int count = 0 ; count < (position - 1) ; count++)
                {
                        if ( tolower(list[count].Title) > tolower(list[count + 1].Title)) 
                        {
                                temp = list[count];
                                list[count] = list[count + 1];
                                list[count + 1] = temp;
                                swap = true;
                        }
                }
        } while (swap);

I wish to use tolower to compare the Title element of two struct arrays. However, compiler won't let me run the program because it says that no matching function to call for tolower.

When I switch the if statement to this:

if ( ::tolower(list[count].Title) > ::tolower(list[count + 1].Title)) 

The "no matching function" message goes away but is replaced by a new one: no viable conversion from 'string' (aka 'basic_string, allocator >') to 'int'.

Lastly I get a consistent error message regarding statments in the body of the if statement, stating no viable overloaded '=' in temp = list[count] and list[count + 1] = temp.

One last detail: list is an array declared as a struct data type. What am I doing wrong?

jshapy8
  • 1,983
  • 7
  • 30
  • 60

2 Answers2

1
  1. tolower works on a single character, not a string. Check out How to convert std::string to lower case?
  2. You are trying to assign a Book to a string (and vice versa). Change the type of temp.
Community
  • 1
  • 1
Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • I changed the data type of `temp` from `string` to `Books` and that eliminated the "no viable overloaded '='" error. Thanks! Unfortunately, the "no viable conversion from 'string' (aka 'basic_string, allocator >') to 'int'" still exists regarding the `if (list[count].Title > list[count + 1].Title)` – jshapy8 Apr 04 '15 at 16:54
  • That's what #1 in my answer is about. – Carl Norum Apr 04 '15 at 16:56
  • So I performed the actions suggested by the link you provided. Thanks for that. The sort seems to be working, kind of. The last entry in my array does not get sorted for some reason. I used `transform(list[count].Title.begin(), list[count].Title.end(), list[count].Title.begin(), ::tolower);` before the if statement. Is it possible that this is an error because of using the bubble sort? Would a selection sort be better? – jshapy8 Apr 04 '15 at 17:16
  • Nevermind! Fixed that issue by removing position - 1 and making just position. Another part of my program made the array off-by-one. Now I just need to figure out how to recover the letters that were capitalized after sorting – jshapy8 Apr 04 '15 at 17:53
0

I take it you're new to C++, first, as Carl Norum mentioned, tolower() works on char's, not strings.

Second, Carl is right about temp being a string (it should be a book), but, there is another large problem, you are copying the "Book" class if you plan on doing it this way. Depending on the size of the class, this could be computationally difficult. If you must "sort" an array multiple times, I would suggest having an array of pointers to speed up the swap function.

Lastly, bubble sort is terrible, don't use it. If you need a set that is always sorted, use a binary search tree or hash. If you must sort an array, the "default" option is Quicksort, which has a plethora of sources online, so I'm not going to post a how to of it.