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;
}
}
}
}
strcmp()forstd::strings? One could also ask other questions, such as why arrays instead of standard containers? Why roll your own sort instead of usingstd::sort?std::string a = "John", b = "Jane" ; strcmp(a, b);The rest is completely irrelevant to the issue you are asking.