How to sorting through a file using C++

I need to sort the containig data following by the students average..
please help, I had 2 days working at it and i have no result!
please help!

Name: sivar
ID:1
Kurdish:87
Arabic:98
English:78
Mathematic:67
Physics:99
Biology:89
Chemical:78
Average=85
;

Name: menas
ID:2
Kurdish:76
Arabic:98
English:87
Mathematic:67
Physics:87
Biology:9
Chemical:878
Average=186
;

Name: Sivar Louis Ya'qub
ID:3
Kurdish:76
Arabic:87
English:76
Mathematic:76
Physics:78
Biology:67
Chemical:98
Average=79
;

Uh huh, what have you tried?
Post your code so far.
For example i have a .txt file that containing these data:

{
Name: sivar
ID:1
Kurdish:87
Arabic:98
English:78
Mathematic:67
Physics:99
Biology:89
Chemical:78
Average=85
;

Name: menas
ID:2
Kurdish:76
Arabic:98
English:87
Mathematic:67
Physics:87
Biology:9
Chemical:878
Average=186

Name: Sandy
ID:3
Kurdish:76
Arabic:87
English:76
Mathematic:76
Physics:78
Biology:67
Chemical:98
Average=79

}

I need a program to read them at same time sort them by them average(from hight to low)..
for example the previous data to be shown on the cmd screen as following:
{
Name: menas
ID:2
Kurdish:76
Arabic:98
English:87
Mathematic:67
Physics:87
Biology:9
Chemical:878
Average=186

Name: sivar
ID:1
Kurdish:87
Arabic:98
English:78
Mathematic:67
Physics:99
Biology:89
Chemical:78
Average=85

Name: Sandy
ID:3
Kurdish:76
Arabic:87
English:76
Mathematic:76
Physics:78
Biology:67
Chemical:98
Average=79
}
you store them in a struct (or class) array, then sort the struct.
They are in a txt file!!!
How I can Store them in a struct?!!
or, if your file always has same format (your previous example has semicolons, latter don't)

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
Name: sivar
ID:1
Kurdish:87
Arabic:98
English:78
Mathematic:67
Physics:99
Biology:89
Chemical:78
Average=85
;

Name: menas
ID:2
Kurdish:76
Arabic:98
English:87
Mathematic:67
Physics:87
Biology:9
Chemical:878
Average=186
;

Name: Sandy
ID:3
Kurdish:76
Arabic:87
English:76
Mathematic:76
Physics:78
Biology:67
Chemical:98
Average=79
;


here, Averages are at line 10,22,34 you can do without a struct
just extract them to std::map<int,int> first int(key) is the Average numbers, 2nd int is line numbers.
then you can make a new file, (i.e. first copy line 13to23, then 1to11, then 25to35)
They are in a txt file!!!
How I can Store them in a struct?!!


it seems easier method than latter method.
the objective of the home work is that you learn how to get data from file to your program.
Oki, thnx alot...
i'll try the i'll show u my result!
input.txt:
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
Name: sivar
ID:1
Kurdish:87
Arabic:98
English:78
Mathematic:67
Physics:99
Biology:89
Chemical:78
Average=85
;

Name: menas
ID:2
Kurdish:76
Arabic:98
English:87
Mathematic:67
Physics:87
Biology:9
Chemical:878
Average=186
;

Name: Sandy
ID:3
Kurdish:76
Arabic:87
English:76
Mathematic:76
Physics:78
Biology:67
Chemical:98
Average=79
;


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
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <map>
#include <cctype>
using namespace std;

int main(){
	ifstream in("input.txt");
	ofstream out("output.txt");
	string s;
	int line=0;
	vector<string> vec(1,"dummy");
	multimap<int,int> M;
	
	while(getline(in, s)){
		line++;
		vec.push_back(s);
		if(line%12==10){
			string temp="";
			for(auto x:s) if(isdigit(x)) temp+=x;
			int key = stoi(temp);
			M.insert(make_pair(key,line));		
		}
	}
	
	auto it = M.rbegin();
	while(it != M.rend()){		
		int i = it->second;
		int start = (int(i/12))*12 +1;
		for(int j=1; j<=12; j++) out << vec.at(start++) << "\n";		
		it++;
	}

	
in.close();
out.close();	
return 0;
}


output.txt:
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
Name: menas
ID:2
Kurdish:76
Arabic:98
English:87
Mathematic:67
Physics:87
Biology:9
Chemical:878
Average=186
;

Name: sivar
ID:1
Kurdish:87
Arabic:98
English:78
Mathematic:67
Physics:99
Biology:89
Chemical:78
Average=85
;

Name: Sandy
ID:3
Kurdish:76
Arabic:87
English:76
Mathematic:76
Physics:78
Biology:67
Chemical:98
Average=79
;

struct method gives you more control, such as Calculating the Average (unlike the input it can be double type)
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
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;

struct data{
	string name;
	int ID;
	//string subjectNames[7];
	int subjectNumbers[7];
	double Average; 
	data(string n1, int a1, int a2[], double a3=0){
		name = n1;
		ID=a1;
		Average=0;
		for(int i=0; i<7 ; i++){
			subjectNumbers[i] = a2[i];
			Average += a2[i];
		}		
		Average = Average/7;  //Calculates Average, instead of Average = a3;
	}
};

int main(){
	ifstream in("input.txt");
	ofstream out("output.txt");
	
	vector<data> vec;
	string s1, s2, name;
	int id, k;
	int numbers[7];
	double Average;	
	
	while(getline(in,s1)){ //Name
		name =s1; // or formatted		
		s2="";
		getline(in,s1); //ID
		for(auto x:s1) if(x >= '0' && x <= '9') s2+= x;
		id = stoi(s2);
		
		for(int i=0; i<7; i++){
			getline(in,s1); // 7 Subjects
			s2="";
			for(auto x:s1) if(x >= '0' && x <= '9') s2+= x;
			numbers[i]= stoi(s2);
		}
		getline(in,s1); // Average
		getline(in,s1); // ";"
		getline(in,s1); // "\n"
				
		vec.emplace_back(data(name,id,numbers));	//without average, subjectName
	}

	
	for(int i=0; i<vec.size(); i++){  // selection sort by Average (descending)
		int highestIndex = i;
		for(int j=i+1; j<vec.size(); j++){
			if(vec[i].Average < vec[j].Average ){
				highestIndex = j;				
			}			
		}
		if(i!=highestIndex) std::swap(vec[i], vec[highestIndex]);
	}
	
	for(auto x:vec){
		out << x.name << "\n"
			<< "ID:" << x.ID << "\n";
		for(int i=0; i<7; i++){
			switch(i){
				case 0: out << "Kurdish:"; break;
				case 1: out << "Arabic:"; break;
				case 2: out << "English:"; break;
				case 3: out << "Mathematics:"; break; // Mathematic in file
				case 4: out << "Physics:"; break;
				case 5: out << "Biology:"; break;
				case 6: out << "Chemistry:"; break; // Chemical in file
			}
			out << x.subjectNumbers[i] << "\n";
		}
		out <<"Average=" << x.Average << "\n"
			<< ";\n\n";		
	}
	
in.close();
out.close();	
return 0;
}

notice that sandy and sivar has fraction point average.

output:
Name: menas
ID:2
Kurdish:76
Arabic:98
English:87
Mathematics:67
Physics:87
Biology:9
Chemistry:878
Average=186
;

Name: sivar
ID:1
Kurdish:87
Arabic:98
English:78
Mathematics:67
Physics:99
Biology:89
Chemistry:78
Average=85.1429
;

Name: Sandy
ID:3
Kurdish:76
Arabic:87
English:76
Mathematics:76
Physics:78
Biology:67
Chemistry:98
Average=79.7143
;

Topic archived. No new replies allowed.