Keeping data for text file

Hey so I have a program in which I enter in a name of a student and insert all the percentages for each class. After that I transfer it into a text document, but here's the problem. After entering in the info, I want to keep the info in there instead of it being overwritten when I enter in new info. Is there a way to do that?
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
  #include<iostream>
#include<string>
#include <vector>
#include <fstream>
using namespace std;

template <class T>
class grades
{
public:
	grades()
	{
		arr = new T[size];
		top = -1;
	}
	void input();
	int *getgrades();
private:
	T* arr;
	int size = 4;
	int top;
	T grade;
};

template <class U>
class student
{
public:
	void names();
	string getname() { return name; };
	string *getclass();
private:
	U name;
	int size = 4;
	string GenEd;
};

template <class U>
void student<U>::names()
{
	cout << "Enter in your name: ";
	getline(cin, name);
}

template <class U>
string *student<U>::getclass()
{
	static string GenEd[4] = { "Math","English","History","Science" };//static to make sure nothing changes
	return GenEd;
}

template <class T>
void grades<T>::input()
{
	cout << "Enter in the grades for the following: " << endl;
	cout << "----------------------------------------" << endl;
	cout << "Math, English, History, Science" << endl;
	cout << "----------------------------------------" << endl;
	for (int i = 0; i < size; i++)
	{
		cin >> grade;
		arr[++top] = grade;
		cout << endl;
	}
}

template <class T>
int *grades<T>::getgrades()
{
	return arr;
}

int main()
{
	char anwser;
	grades<int>gr;
	student<string>students;
	string* GenEd = students.getclass();
	int* grade = gr.getgrades();
	do {
		ofstream myfile;
		myfile.open("ListGrade.txt");
		students.names();
		gr.input();
		myfile <<"Name: " <<students.getname() << endl;
		cout << "------------------------" << endl;
		for (int i = 0; i < 4; i++)
		{
			myfile << GenEd[i]<<":" << grade[i]<<"%"<<endl;
		}
		gr.getgrades();
		myfile.close();
		cout << "wish to keep going?(Y/N): ";
		cin >> anwser;
		cin.ignore();
	} while (anwser == 'Y' || anwser == 'y');
}
Last edited on
open the file to append to it.
ifstream.open(name, ios::ate) //this idea at line 82...
Last edited on
I believe you mean fstream.open(name, ios::app) but thanks it helped!

Now I have one more problem it seems when I enter in data. Once I add in the data and answer yes to keep going, the grades stay the same as the first entry. For example, I enter in the name John Doe and enter in the grades for each class percentage wise. Say I put 67,98,90,85 for John. Then after that I answer yes to keep going and enter in a separate student with their own grades. The name here is Jane Doe with grades that are 75,78,86,67. I look at the info written in the text file to see that the names changed as they should, but not the array of grades. They stay the same as the first entry.

Name:John Doe
Math:67
English:98
History:90
Science:85

Name:Jane Doe
Math:67
English:98
History:90
Science:85


what value do you think top has for the variable gr for when you input the data for the second student?
Must I use clear() or something when answering yes? I'm thinking off the top of my head at the moment.
Using 'top' at all is pointless. Just use the loop variable since it's counting up to size anyway.

And the file should presumably be opened before the loop and closed afterwards. Then you don't need to open it in append mode.
Oh I see! Thank you for that!
Topic archived. No new replies allowed.