Sort a structure

Hey everyone. The program I'm working on is going well and I'm just about done, largely thanks to all of you. There is only one more thing I need to do, and that is sort it. What I need to do is, after the file is opened, entries are made, etc., allow the user to select an option that calls a function to sort the entries alphabetically according to last name (the first element in the vector). For clarity, this is the entire code for my program:

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <fstream>
#include <algorithm>

using namespace std;

struct Person
{
	string firstName;
	string lastName;
	int age;
};

void getNames(vector <Person> &people)
{
	Person aPerson;
	while (aPerson.firstName != "quit")
	{
		cout << "Enter first name, last name, and age: ";
		cin >> aPerson.firstName;
		if(aPerson.firstName=="quit") break;
		cin >> aPerson.lastName >> aPerson.age;
		people.push_back(aPerson);
	}
}

void display(const vector <Person> &people)
{
	for (int i = 0; i < people.size(); i++)
	{
		cout << "Name: " << people[i].lastName << ", " << people[i].firstName << ". Age: " << people[i].age << endl;
	}
}

void open(vector <Person> &people)
{
	ifstream fin("names.txt");
	Person aPerson;
	string tmp;
	stringstream ss;
	
	while(!fin.eof())
	{
		getline(fin, aPerson.lastName,';');
		
		getline(fin, aPerson.firstName,';');
	
  		getline(fin, tmp); 
  		ss << tmp;
  		ss >> aPerson.age;
  		
  		people.push_back(aPerson);
	}
}

void save(ofstream &fout, vector <Person> people)
{
	fout.open("names.txt");
	for(int i=0; i < people.size(); i++)
	{
		fout << people[i].lastName << ';';
  		fout << people[i].firstName << ';';
  		fout << people[i].age;
  		if(i < people.size() -1)
  		fout << endl;
  }
}

void remove(ofstream &fout, vector <Person> &people)
{
	int loc=people.size();
	string remove;
	cout << "Enter the last name of who you want to remove: ";
	cin >> remove;
	for (int i=0; i < people.size(); i++)
	{
		if (people.at(i).lastName == remove)
		{
			loc = i;
		}
	}	
	
	if(loc!=people.size()){
		for (int j = loc; j < people.size()-1; j++)
		{
			swap(people[j], people[j+1]);
		}
		people.pop_back();
		//people.erase(people.begin(), people.end()); (erases the entire file if used instead)
		cout << "Entry Removed." << endl;
	}
	else cout << "Name not found." << endl;
	
	fout.open("names.txt");
	for(int i=0; i < people.size(); i++)
	{
		fout << people[i].lastName << ';';
  		fout << people[i].firstName << ';';
  		fout << people[i].age;
  		if(i < people.size() -1)
  		fout << endl;
	}
  	cout << endl;
}

void sortNames(vector <Person> &people)
{
	sort(people.begin(), people.end());
}
			

int main()
{
	ofstream fout;
	vector <Person> people;
	cout << "Welcome to the Names Catalogue. Please start by opening the file." << endl;
	cout << endl;
	int sel;
	while (true)
	{
		cout << "--------------------------" << endl;
		cout << "What would you like to do?" << endl;
		cout << "--------------------------" << endl;
		cout << "Enter 1 add entries." << endl;
		cout << "Enter 2 to display entries." << endl;
		cout << "Enter 3 to save entries." << endl;
		cout << "Enter 4 to open file." << endl;
		cout << "Enter 5 to remove an entry." << endl;
		cout << "Enter 6 to quit." << endl;
		cout << "--------------------------" << endl;
		cout << "Enter selection: ";
		cin >> sel;
		cout << "--------------------------" << endl;
		switch (sel)
		{
			case 1: getNames(people); break;
			case 2: display(people); break;
			case 3: save(fout, people); break;
			case 4: open(people); break;
			case 5: remove(fout, people); break;
			case 6: sortNames(people); break;
			case 7: cout << "Goodbye."; break;
			default: cout << "Error: Incorrect input. Please try again." << endl; break;
		}
		if (sel == 7) break;
	}
	fout.close();
}


I will use prototypes, but I prefer to write it this way first, then make them later. For now, I need to get that sort to work. Right now it errors rather massively, opening a new code that highlights if (*__i < *__first) and I have no idea what that means. I know I probably need to use a normal vector and not a structure, but I'm not sure how to go about doing that.
Topic archived. No new replies allowed.