Help with saving the data in the array

Hello,
I have a project to make array, and I want to save the input data when is enter, so you don't need to enter it again.
I made option N7 to save my data, but I don't know how to make the code for storing the information that you enter for every student, and when you open the program again you can enter a new student information or you can work with the old info that you enter before.

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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
 #include <iostream>
#include <windows.h>
#include <conio.h>
#include <cstdlib>
#include <fstream>

using namespace std;


int N=0;
struct StudentStruct
{
	unsigned int FacultativeNumber;
	unsigned int Age;
	unsigned Grade;
	unsigned short int Sex;	
}
	Student[55];

void Enter(void)
	{
		char Answer;
		if(N<55)
		{
			cout<<"\n New student number"<<1+N<<":"<<endl;
			cout<<"Facultative Number: "; 
				cin>>Student[N].FacultativeNumber;
			cout<<"Age: ";
				cin>>Student[N].Age;
			cout<<"Grade: ";
				cin>>Student[N].Grade;
			cout<<"Sex (1-male, 0-female): ";
				cin>>Student[N].Sex;
			cout<<endl; N++;
			do
			{
				cout<<"One more Student? ('y'-Yes, 'n'-No): ";
				cin>>Answer;
			}
			while(Answer!='y'&&Answer!='n');
			if(Answer=='y') Enter ();
		}
		cout<<endl<<endl;
	}

	void malefemale(void)
	{
		int c, countmale=0, countfemale=0;
		unsigned int male[55][2], female[55][2];
		for(c=0;c<N;c++)
		{
			if(Student[c].Sex)
			{
				male[countmale][0]=Student[c].FacultativeNumber;
				male[countmale][1]=Student[c].Age;
				countmale++;				
			}
			else
			{
				female[countfemale][0]=Student[c].FacultativeNumber;
				female[countfemale][1]=Student[c].Age;
				countfemale++;
			}
		}
		float SumAgemale=0.0f,SumAgefemale=0.0f;
		for(c=0;c<countmale;c++) SumAgemale+=(float)male[c][1];
		for(c=0;c<countfemale;c++) SumAgefemale+=(float)female[c][1];
		cout<<"Male:"<<endl;
		cout<<"FacultativeNumber:\tAge:"<<endl;
		for(c=0;c<countmale;c++) cout<<male[c][0]<<"\t\t\t"<<male[c][1]<<endl;
		cout<<"Average Age: "<<SumAgemale/(float)countmale;
		cout<<endl<<endl;
		cout<<"female: "<<endl;
		cout<<"FacultativeNumber: \tage: "<<endl;
		for(c=0; c<countfemale; c++) cout<<female[c][0]<<"\t\t\t"<<female[c][1]<<endl;
		cout<<"Average Age: "<<SumAgefemale/(float)countfemale;
		cout<<endl<<endl;
	}
		void malefemale2(void)
	{
		int c, countmale=0, countfemale=0;
		unsigned int male[55][2], female[55][2];
		for(c=0;c<N;c++)
		{
			if(Student[c].Sex)
			{
				male[countmale][0]=Student[c].FacultativeNumber;
				male[countmale][1]=Student[c].Grade;
				countmale++;				
			}
			else
			{
				female[countfemale][0]=Student[c].FacultativeNumber;
				female[countfemale][1]=Student[c].Grade;
				countfemale++;
			}
		}
		float SumGrademale=0.0f,SumGradefemale=0.0f;
		for(c=0;c<countmale;c++) SumGrademale+=(float)male[c][1];
		for(c=0;c<countfemale;c++) SumGradefemale+=(float)female[c][1];
		cout<<"Male:"<<endl;
		cout<<"FacultativeNumber:\tGrade:"<<endl;
		for(c=0;c<countmale;c++) cout<<male[c][0]<<"\t\t\t"<<male[c][1]<<endl;
		cout<<"Average Grade: "<<SumGrademale/(float)countmale;
		cout<<endl<<endl;
		cout<<"female: "<<endl;
		cout<<"FacultativeNumber: \tGrade: "<<endl;
		for(c=0; c<countfemale; c++) cout<<female[c][0]<<"\t\t\t"<<female[c][1]<<endl;
		cout<<"Average Grade: "<<SumGradefemale/(float)countfemale;
		cout<<endl<<endl;
	}
	
	void Search (void)
	{
		bool Exist;
		char Answer;
		unsigned int SearchedFacultativeNumber;
		int c;
		do
		{
			cout<<"\n\nFacultativeNumber for searching: "; cin>>SearchedFacultativeNumber;
			Exist=false;
			for(c=0; c<N;c++)
				if(Student[c].FacultativeNumber==SearchedFacultativeNumber)
				{
					Exist=true;
					cout<<"\nNumber in row: "<<1+c<<endl;
					cout<<"Age: "<<Student[c].Age<<endl;
					cout<<"Grade: "<<Student[c].Grade<<endl;
					cout<<"Sex: ";
					if(Student[c].Sex) cout<<"male"<<endl;
					else               cout<<"female"<<endl;
				}
			if(!Exist) cout<<"Student with FacultativeNumber "<<SearchedFacultativeNumber<<"Not Exist."<<endl;
			cout<<endl;
			do
				{
					cout<<"One more Student? ('y'-yes, 'n'-no): ";
					cin>>Answer;
				}
				while(Answer!='y'&&Answer!='n');		   
		}
		while(Answer!='n');
		cout<<endl<<endl;
	}
	
	void MostYoungestStudent()
		
		
	{
	
		unsigned int MinAge;
		int cMinAge, c, cc;
		c=0;
		while(Student[c].Sex&&c<N) c++; 
		if(c<N)
		{
			cMinAge=c;
			MinAge=Student[c].Age;
			for(cc=c+1;cc>N; cc++)
			if(!Student[cc].Sex&&Student[cc].Age<MinAge)
			{
				MinAge=Student[cc].Age;
				cMinAge=cc;
			}
		cout<<endl<<"Youngest student's Facultative Number is "<<Student[cMinAge].FacultativeNumber<<endl<<"And is"<<Student[cMinAge].Age<<" Age."<<endl<<endl;

		}
		else
			cout<<endl<<"There are not such students. "<<endl<<endl;
	}
	
	void SortAscending()
	{
		
		int x,y;
		for(x=0;x<N;x++)
		for(y=x+1;y<N;y++)
			
				if(Student[y].Age<Student[x].Age)
                swap(Student[x],Student[y]);
				
								
		for(x=0;x<N;x++)
		
		cout<<endl<<"Age: "<<1+x<<":"<<endl<<"Facultative Number: "<<Student[x].FacultativeNumber<<endl<<"Student #"<<Student[x].Age<<endl<<"Sex: "<<((Student[x].Sex) ?"male.":"female.")<<endl<<endl;
			
	}
	
	void Save ()
	{
		
	}
	
	int main()
	{
		int Choice;
		SetConsoleCP(1251);SetConsoleOutputCP(1251);
		do
		{
			do
			{
				cout<<"\n\n1. Add Students\n" 
					<<"2. Male and Female - Average age\n"
					<<"3. Male and Female - Average grade\n"
					<<"4. Searching studentst using Facultative Number\n"
					<<"5. The Youngest Female Student is: \n"
					<<"6. Sort Students Ascending by Age \n\n"
					<<"7. Save\n\n"
					<<"0. End\n\n\n";
				cout<<"> ";cin>>Choice;
				}	
			while(Choice<0||Choice>7);
			switch(Choice)
			{
				case 1:
						Enter();
				break;
				case 2:
						malefemale();
				break;
				case 3:
						malefemale2();
				break;
				case 4:
						Search();
				break;
				case 5:
						MostYoungestStudent();
				break;
				case 6:
						SortAscending();
				break;
				case 7:
						Save();
				break;
			}
		}
		while(Choice!=0);
		return 0;
	}.
To save your data you need to create an ofstream object, write first the number of students into it and then write all the student data.
Here is a tutorial:
http://www.cplusplus.com/doc/tutorial/files/
Thanks! :)
Topic archived. No new replies allowed.