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?

2 Answers 2

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.
Sign up to request clarification or add additional context in comments.

4 Comments

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)
That's what #1 in my answer is about.
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?
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
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.