Sort records in struct

Hello!I need to sort students by name and by age. But I just can't figure out how to do it. Basically I need a function like chkinfo() to list all records but sort the names alphabetically.
And I know the goto I have is bad. How can I change it?

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
#include <...>
const int N = 30; //max students
const char students[] = "students.dat";
struct student
{
	char fnum[9];
	char name[30];
	char sex;
	int age;
	int marks[4];
	double srusp;
	char* status="NoPass";
};
int n;
std::fstream fp;
void menu();
void crstudent(struct student stud[]);
void chkinfo(struct student stud[]);
void srtname(struct student stud[]);
//void srtage(struct student stud[]);
void addstudent();
void back();
int main()
{
	menu();
}
void menu()
{...}
void crstudent(struct student stud[])
{
	fp.open("students.dat", std::ios::out | std::ios::binary);
	if (!fp)
	{
		std::cerr << std::endl << "Error creating the file!";
		exit(1);
	}
back:
	std::cout << "Input the number of students you wish to add: ";
	std::cin >> n;
	char answer;
	for (int i = 0; i < n; i++)
	{
		std::cout << "Input student " << i + 1 << " faculty number: ";
		std::cin.ignore();
		std::cin.getline(stud[i].fnum, 9, '\n');
		std::cout << "Input student's name: ";
		std::cin.getline(stud[i].name, 30, '\n');
		std::cout << "Input student's age: ";
		std::cin >> stud[i].age;
		std::cout << "Input student's sex - M or F: ";
		std::cin >> stud[i].sex;
		stud[i].sex = std::toupper(stud[i].sex);
		stud[i].srusp = 0;
		std::cout << "Do you wish to add marks? - Y or N: ";
		std::cin >> answer;
		answer = std::toupper(answer);
		if (answer == 'N')break;
		std::cout << "Input student's marks: \n";
		for (int k = 0; k < 4; k++)
		{
			std::cout << "Input mark number " << k + 1 << ": ";
			std::cin >> stud[i].marks[k];
			stud[i].srusp += stud[i].marks[k];
		}
		stud[i].srusp = stud[i].srusp / 4.00;
	}
	if (n == 1)
	{
		std::cout << "\nDo you wish to add more students? Y or N: ";
		std::cin >> answer;
		answer = std::toupper(answer);
		if (answer == 'Y')goto back;
		std::cout << std::endl;
	}
	fp.write((char*)stud, n * static_cast<int64_t>(sizeof(student)));
	fp.close();
	system("CLS");
}
void chkinfo(struct student stud[])
{
	std::streamoff pos;
	student s;
	fp.open("students.dat", std::ios::in | std::ios::binary);
	if (!fp)
	{
		std::cerr << std::endl << "File does not exist!";
		exit(1);
	}
	fp.seekg(0L, std::ios::end);
	pos = fp.tellg();
	fp.close();
	fp.open("students.dat", std::ios::in | std::ios::binary);
	fp.seekg(0);
	for (int i = 0; i < (pos / sizeof(student)); i++)
	{
		fp.read((char*)&s, sizeof(student));
		std::cout << std::endl << "Faculty number: " << stud[i].fnum;
		std::cout << std::endl << "Name: " << stud[i].name;
		std::cout << std::endl << "Age: " << stud[i].age;
		std::cout << std::endl << "Sex: " << stud[i].sex;
		std::cout << std::endl << "Average result is: " << stud[i].srusp;
		std::cout << std::endl;
	}
	fp.close();
	back();
}
void addstudent()
{...}
void back()
{...}
void srtname(struct student stud[])
{

	back();
}
Last edited on
The preferable way to sort is with std::sort http://www.cplusplus.com/reference/algorithm/sort/

If I had a bunch of students:
1
2
3
4
5
6
7
8
9
10
11
12
13
std::vector<student> stud; // no need to put 'struct' everywhere in C++
// fill the stud somehow

// sort stud by 'name'
std::sort( stud.begin(), stud.end(),
           [](const student& lhs, const student& rhs)
             { return strcmp(lhs.name, rhs.name) < 0; }
         );

// show name
for ( const auto& s : stud ) {
  std::cout << s.name << '\n';
}
How do I fill the stud in my case?
1
2
3
student s;
//...fill s
stud.push_back( s );
Topic archived. No new replies allowed.