1

I have to write a program that sorts 10 people by height, then by last name. I've got the height down, but i can't get the last name sort to work. I'm trying to use strcmp for it. Any time I try to run it though, it flags an error at the strcmp saying, "[Error] cannot convert 'std::string {aka std::basic_string}' to 'const char*' for argument '1' to 'int strcmp(const char*, const char*)'" I'm using strcmp because this is for a school assignment and I am limited by my knowledge of c++ and what my professor allows us to use

int main()
{
    const int SIZE = 10;
    int count = 0;
    bool flag = true;
    string fileName;
    ifstream inputFile;
    string firstName[SIZE];
    string lastName[SIZE];
    int height[SIZE];

    cin >> fileName;
    inputFile.open(fileName.c_str());

    while(count < 10)
    {
        inputFile >> firstName[count];
        inputFile >> lastName[count];
        inputFile >> height[count];
        count++;
    }
    //Sort based on height
    for(int max = SIZE - 1; max > 0 && flag; max--)
    {
        flag = false;

        for(int line = 0; line < max; line++)
        {
            if(height[line] > height[line + 1])
            {
                swap(height[line], height[line + 1]);
                swap(firstName[line], firstName[line + 1]);
                swap(lastName[line], lastName[line + 1]);
                flag = true;
            }
        }
    }
    //Sort based on last name if heights are equal
    for(int max = SIZE - 1; max > 0 && flag; max--)
    {
        flag = false;

        for(int line = 0; line < max; line++)
        {
            if(height[line] == height[line + 1])
            {
                if(strcmp(lastName[line], lastName[line + 1]) > 0)
                {
                    swap(height[line], height[line + 1]);
                    swap(firstName[line], firstName[line + 1]);
                    swap(lastName[line], lastName[line + 1]);
                    flag = true;
                }
            }
        }
    }
3
  • 3
    Why use strcmp() for std::strings? One could also ask other questions, such as why arrays instead of standard containers? Why roll your own sort instead of using std::sort? Commented Nov 25, 2019 at 16:40
  • 1
    Please create a minimal reproducible example Your whole problem can be reduced to 2 lines of code: std::string a = "John", b = "Jane" ; strcmp(a, b); The rest is completely irrelevant to the issue you are asking. Commented Nov 25, 2019 at 16:42
  • @FredLarson I'm still fairly new to c++ and this is for a school assignment. I'm just using the methods I've been introduced to so far Commented Nov 25, 2019 at 16:46

1 Answer 1

3

If you insist on using the old strcmp function, then you should pass lastName[line].c_str() and lastName[line+1].c_str()as its argument(s). However, you'd be better off using the std::string::compare() function provided by the STL:

if (lastName[line].compare(lastName[line + 1]) > 0)

This does much the same thing.

Or even simpler (as Fred Larson has suggested):

if (lastName[line] > lastName[line+1])
Sign up to request clarification or add additional context in comments.

5 Comments

Or just if (lastName[line] > lastName[line+1])
@FredLarson Shorter and sweeter code, I agree, but it does the same thing. From cppreference on string operators: All comparisons are done via the compare() member function (which itself is defined in terms of Traits::compare())
No question it does the same thing, but it is shorter and sweeter, as you said. Crucially, I feel it more clearly expresses the intent and is thus more readable.
@FredLarson I would add it as an option, but I don't want to steal your thunder! 😊
There, take my thunder.

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.