Need help with data handling

Hey I need some help with file handling. I am making a math quiz. I am stuck on the last task of my work. This is the task specification. (any advice would be massively appreicanted!!) At the moment the code just saves the score to a text file depending on what class they are in. However if a student takes the quiz multiple times under the same name it will just create a new line. I want it to save the score on the same line like this.
Bob scored : 1,2,3
instead of this
bob scored 1
bob scored 2
bob scored 3
and to make it even more complicated it can't store more then 3 numbers per name. I am really really really stuck.

The teacher wants to use the results from students taking these quizzes to log their performance. The
system should store the last three scores for each student. The teacher would like to be able to output
the results of the quiz for a particular class, sorted:
• in alphabetical order with each student’s highest score for the tests
• by the highest score, highest to lowest
• by the average score, highest to lowest.

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

int answer;
int question;
int correct;
string name;
char classNo;

void getinfo();

void quiz();

int main(){

	getinfo();

	return 0;

}

void getinfo(){

	cout << "What class are you in?" << endl;
	cin >> classNo;
	cout << "What is your name?" << endl;
	cin >> name;

	if (classNo == '1'){
		cout << "You are in Mr Smith's class" << endl;
		quiz();
	}
	else if (classNo == '2'){
		cout << "You are in Mrs Heartfield's class" << endl;
		quiz();
	}
	else if (classNo == '3'){
		cout << "You are in Mr Neacy's class" << endl;
		quiz();
	}
	else { cout << "This class does not exist!"; getinfo(); }
}

void quiz(){



	while (question < 10){

		int A = rand() % 10 + 1;
		int B = rand() % 10 + 1;
		int operNum = rand() % 3 + 1;


		if (operNum == 1){
			answer = A + B;
			cout << "What is " << A << "+" << B << endl;
		}
		else if (operNum == 2){
			answer = A - B;
			cout << "What is " << A << "-" << B << endl;
		}
		else if (operNum == 3){
			answer = A * B;
			cout << "What is " << A << "*" << B << endl;
		}

		int userInput;
		cin >> userInput;

		if (userInput == answer){
			cout << "You are correct!" << endl;
			question = question + 1;
			correct = correct + 1;
		}
		else{
			cout << "You are incorrect" << endl;
			question = question + 1;
		}

	}

	if (correct > 5){
		cout << "Welldone you got " << correct << " / 10" << endl;
	}
	else if (correct == 0){
		cout << "You got nothing correct" << endl;
	}
	else{
		cout << "You only got " << correct << " / 10" << endl;
	}

	ofstream save;
	if (classNo == '1'){
		save.open("class1.txt", std::ios_base::app);
		save << name << " Scored: " << correct << endl;
		save.close();
	}
	else if (classNo == '2'){
		save.open("class2.txt", std::ios_base::app);
		save << name << " Scored: " << correct << endl;
		save.close();
	}
	else {
		save.open("class3.txt", std::ios_base::app);
		save << name << " Scored: " << correct << endl;
		save.close();
	}

	int x;		//just to stop the window from closing instantly
	cin >> x;
}
What I suggest is read the output file before you write it. Find out if the current student is already in the file, otherwise append. If it's in the file, at line n, store all the lines in memory, modify only line n, then write all lines to the file. How to modify line n? Keep the name, see if less then 3 scores. If less, append current score to the line (don't forget the comma). If 3 scores, write the second, third and current
Sounds like a good idea, but still can't get it working :(
Exactly what changes did you make?
Topic archived. No new replies allowed.