reading a file and sorting it alphabetically

hello there,

i am trying to read a file and sorting it alphabetically, i do not know how to tackle this issue.

so i am reading a file that contains , States, counties, and cities. and i need to sort the State alphabetically. can you please guide on how to solve this?



Define a structure that contains three strings; the state, the county and the city.

Read in the data, storing it in structures. Put each new structure into a vector of the structures.

When you're read them all in, use the sort function ( # include <algorithms> ) . You will need to provide the function for comparing your structures to the sort function.

Implement as much of what you understood of what I just said, and then come back with the first bit you're stuck on.
thank you for your help.

so i tried this one and it did not work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void sorting(struct state* array, int x){


		    int narray = sizeof(array[x].name)/sizeof(array[0].name);

		        sort(array[x].name, array[x].name + narray);

			    for(int i = 0; i < narray; ++i)
			               cout << array[i].name<< endl;




}

here a sample of how i read from the file:
1
2
3
File >> array[i].name;
   File >> array[i].population;
   File >> array[i].counties;

1
2
3
4
5
6
struct state {
   string name; //name of state
   struct county *c; //name of counties
   int counties; //number of counties in state
   int population; //total population of state
}; 


i get a lot of errors:

main.cpp:193:45: error: invalid operands to binary expression ('string' (aka 'basic_string<char, char_traits<char>, allocator<char> >') and 'int')
sort(array[x].name, array[x].name + narray);
~~~~~~~~~~~~~ ^ ~~~~~~
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/string:3987:1: note: candidate template ignored: deduced conflicting types for parameter '_CharT' ('char' vs. 'int')
operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/iterator:640:1: note: candidate template ignored: could not match 'reverse_iterator<type-parameter-0-0>' against 'int'
operator+(typename reverse_iterator<_Iter>::difference_type __n, const reverse_iterator<_Iter>& __x)
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/iterator:1044:1: note: candidate template ignored: could not match 'move_iterator<type-parameter-0-0>' against 'int'
operator+(typename move_iterator<_Iter>::difference_type __n, const move_iterator<_Iter>& __x)
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/iterator:1400:1: note: candidate template ignored: could not match '__wrap_iter<type-parameter-0-0>' against 'int'
operator+(typename __wrap_iter<_Iter>::difference_type __n,
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/string:3939:1: note: candidate template ignored: could not match 'basic_string<type-parameter-0-0, type-parameter-0-1, type-parameter-0-2>' against 'int'
operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/string:3952:1: note: candidate template ignored: could not match 'const _CharT *' against 'string' (aka 'basic_string<char, char_traits<char>, allocator<char> >')
operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/string:3964:1: note: candidate template ignored: could not match 'basic_string<type-parameter-0-0, type-parameter-0-1, type-parameter-0-2>' against 'int'
operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/string:3975:1: note: candidate template ignored: could not match 'const _CharT *' against 'int'
operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
^
1 error generated.
make[2]: *** [build/Debug/GNU-MacOSX/main.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2

BUILD FAILED (exit value 2, total time: 2s)
Last edited on
Topic archived. No new replies allowed.