Hi I'm trying to sort user entered names alphabetically. I'm getting a lot of errors and my code doesn't compile. I'm very exhausted so I'm not sure if I'm just overlooking something simple.the errors are "no conversion from string to const char exists" on the input array. any advice is appreciated!
OP: your sort_n() function looks funny, int j goes to the inner loop counter and int n is unused. You need to re-look at that one.
As the previous post mentions, default string comparison is already alphabetical; so once you have the vector<string> string_array you could just:
Or, to fix the problems in the original code. Pass the string vector by reference, don't use strcmp(), simply compare the strings using the > operator. Make temp a string, not a char *. Compare elements i and j, not i and i.
1 2 3 4 5 6 7 8 9 10 11 12 13
void sort_n(vector<string> & input)
{
for (size_t i = 0; i < input.size(); i++)
{
for (size_t j = i + 1; j < input.size(); j++)
if (input[i] > input[j])
{
string temp = input[i];
input[i] = input[j];
input[j] = temp;
}
}
}