Trying to sort names alphabetically?

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!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void sort_n(string *input, int j, int n)
{
	for (int i = 0; i < 10; i++) {
		for (int j = i + 1; j < 10; j++)
			if (strcmp(input[i], input[i]) > 0) {
			char* temp = input[i];
			input[i] = input[j];
			input[j] = temp;
			}
	}
}

vector<string> string_array;
	string input;
	for (int j = 0; j < 10; j++) {
		cout << "enter some names" << endl;
		cin >> input[j];
		string_array.push_back(input);
	}

	int number_of_names = string_array.size();

	for (int i = 0; i < number_of_names; i++) {
		cout << "name #" << i << "is: " << string_array[i] << endl;
	}
Last edited on
String comparison is already alphabetical, no need to convert them to C arrays.
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:
1
2
#include <algorithm>
std::sort(string_array.begin(), string_array.end())

And you can also define your custom sorting criterion, say sorting by size. Following program is an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

struct size_sort
{
    bool operator () (const std::string& lhs, const std::string& rhs)
    {
        return lhs.size() < rhs.size();
    }
};
int main()
{
    std::vector<std::string> string_array {"today", "is", "winter", "solstice"};
    std::sort(string_array.begin(), string_array.end());
    std::cout << "Default sort: \n";
    for (auto& elem: string_array)
    {
        std::cout << elem << " " ;
    }
    std::cout << '\n';
    std::sort(string_array.begin(), string_array.end(), size_sort());//can also use lambda
    std::cout << "Size sort: \n";
    for (auto& elem: string_array)
    {
        std::cout << elem << " " ;
    }
    std::cout << '\n';
}

Output
1
2
3
4
Default sort:
is solstice today winter
Size sort:
is today winter solstice

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;
            }
    }
}
thank you everyone! i also get a popup debug assertion fail and im not sure why
Could you clarify, what exactly is the message in the popup please.
Topic archived. No new replies allowed.