Array of struct help

Im having trouble figuring out how to take a previously done program and converting the entire thing int an array of structure. Any hints and clues that can help me out will be much appreciated.

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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# include <fstream>
# include <iomanip>
# include <string>
# include <cmath>
# include <cctype>

using namespace std;

const int STUIDMIN = 1111;
const int STUIDMAX = 9999;
const int EXAMMIN = 0;
const int EXAMMAX = 100;
const int MAXA = 100;
const int MINA = 90;
const int MAXB = 89;
const int MINB = 80;
const int MAXC = 79;
const int MINC = 70;
const int MAXD = 69;
const int MIND = 60;
const int MAXF = 59;
const int MINF= 0;
const char SEXF = 'F';
const char SEXM = 'M';
const int MAX_SIZE = 10;
enum gradeType {A,B,C,D,F};

struct ExamData
{
	float e1;
	float e2;
	float e3;
	int examAvg;
	int totA;
	int totB;
	int totC;
	int totD;
	int totF;
	float highest;
	float lowest;
	gradeType stuGrade;
};

struct StuData
{
	string name;
	int id;
	char sex;
	int totFe;
	int totMa;
	float perFe;
	float perMa;
	ExamData exams;
};
StuData student;

void printHead(ofstream&);

char sexUpper(StuData);
void printData(ofstream&, StuData);
bool validData(StuData);
int avg(StuData);
gradeType grade(StuData&);
int sexCount(StuData&);
void examHighLow(StuData&);
void printAvgAndGrade(ofstream&,StuData);
void printInvalidData(ofstream&);
void printNoValidData(ofstream&);
void printStat(ofstream&, StuData);

int main()
{
  ifstream inFile;
  ofstream outFile;

  inFile.open("in.data");
  outFile.open("out.data");

  if(outFile.fail() || inFile.fail())
   {
    outFile << "Files have failed, check for errors." << endl;
    return 1;
   }

  outFile.setf(ios::fixed);
  outFile.setf(ios::showpoint);
  outFile.precision(2);

  printHead(outFile);

  student.exams.totA = 0;
  student.exams.totB = 0;
  student.exams.totC = 0;
  student.exams.totD = 0;
  student.exams.totF = 0;
  student.exams.highest = -1;
  student.exams.lowest = -1;
  student.totMa = 0;
  student.totFe = 0;
  student.perFe = 0;
  student.perMa = 0;

  inFile >> student.name >> student.id >> student.sex >> student.exams.e1 >> student.exams.e2 >> student.exams.e3;

  while (inFile)
  {
    student.sex = sexUpper(student);

    printData(outFile, student);

    if (validData(student))
    {

      student.exams.examAvg = avg(student);

      student.exams.stuGrade = grade(student);

      sexCount(student);

      examHighLow(student);

      printAvgAndGrade(outFile, student);
    }

    else
      printInvalidData(outFile);

    inFile >> student.name >> student.id >> student.sex >> student.exams.e1 >> student.exams.e2 >> student.exams.e3;
  }

  if(student.exams.highest + student.exams.lowest == -2)

    printNoValidData(outFile);


  else
    printStat(outFile, student);

  inFile.close();
  outFile.close();

  return 0;
}

 void printHead(ofstream& outFile)
 {
   outFile << "~~Stud Exam Report~~" << endl;
   outFile << left << setw(15) << "Name" << setw(15) << "Stud ID" << setw(10) << "Sex"
           << setw(10) << "Exam 1" << setw(10) << "Exam 2" << setw(10) << "Exam 3"
           << setw(15) << "Avg of Exams" << setw(10) << "Grade" << endl;
   outFile << left << setw(15) << "----" << setw(15) << "-------" << setw(10) << "---"
           << setw(10) << "------" << setw(10) << "------" << setw(10) << "------"
           << setw(15) << "------------" << setw(10) << "-----" << endl;
 }

 char sexUpper(StuData student)
 {
   return toupper(student.sex);
 }

 void printData(ofstream& outFile, StuData student)
 {
    outFile << left << setw(15) << student.name << setw(15) << student.id << setw(10) << student.sex
            << setw(10) << student.exams.e1 << setw(10) << student.exams.e2 << setw(10) << student.exams.e3;
 }

 bool validData(StuData student)
 {
   if((student.id >= STUIDMIN && student.id <= STUIDMAX)&& (student.sex == SEXF || student.sex == SEXM) &&
      (student.exams.e1 >= EXAMMIN && student.exams.e1 <= EXAMMAX) &&  (student.exams.e2 >= EXAMMIN && student.exams.e2 <= EXAMMAX) &&
      (student.exams.e3 >= EXAMMIN && student.exams.e3 <= EXAMMAX))

     return true;

   else
     return false;
 }

 int avg(StuData student)
 {
   return ceil((student.exams.e1 + student.exams.e2 + student.exams.e3)/3);
 }

 gradeType grade(StuData& student)
 {
   switch (student.exams.examAvg)
   {
      case 90 ... 100:
      {
        student.exams.totA++;
        return A;
	break;
      }
      case 80 ... 89:
      {
        student.exams.totB++;
        return B;
        break;
      }
      case 70 ... 79:
      {
        student.exams.totC++;
        return C;
	break;
      }
      case 60 ... 69:
      {
        student.exams.totD++;
        return D;
	break;
      }
      case 0 ... 59:
      {
        student.exams.totF++;
        return F;
	break;
      }
   }
 }

 int sexCount(StuData& student)
 {
   if(student.sex == 'F')
    return student.totFe++;
   else
     return student.totMa++;
 }

 void examHighLow(StuData& student)
 {
   if(student.exams.highest == -1)
     student.exams.highest = student.exams.e1;
   else if (student.exams.highest < student.exams.e1)
     student.exams.highest = student.exams.e1;
   else if (student.exams.highest < student.exams.e2)
     student.exams.highest = student.exams.e2;
   else if (student.exams.highest < student.exams.e3)
     student.exams.highest = student.exams.e3;

   if(student.exams.lowest == -1)
     student.exams.lowest = student.exams.e1;
   else if (student.exams.lowest > student.exams.e1)
     student.exams.lowest = student.exams.e1;
   else if(student.exams.lowest > student.exams.e2)
     student.exams.lowest = student.exams.e2;
   else if(student.exams.lowest > student.exams.e3)
     student.exams.lowest = student.exams.e3;

 }

 void printAvgAndGrade(ofstream& outFile, StuData student)
 {
   switch(student.exams.stuGrade)
   {
     case A:
     {
       outFile << setw(15) << student.exams.examAvg;
       outFile << setw(10) << "A" << endl;
       break;
     }
     case B:
     {
       outFile << setw(15) << student.exams.examAvg;
       outFile << setw(10) << "B" << endl;
       break;
     }
     case C:
     {
       outFile << setw(15) << student.exams.examAvg;
       outFile << setw(10) << "C" << endl;
       break;
     }
     case D:
     {
       outFile << setw(15) << student.exams.examAvg;
       outFile << setw(10) << "D" << endl;
       break;
     }
     case F:
     {
       outFile << setw(15) << student.exams.examAvg;
       outFile << setw(10) << "F" << endl;
       break;
     }
   }
}

 void printInvalidData(ofstream& outFile)
 {
    outFile << setw(15) << "~~Invalid Data~~" << endl;
 }

 void printNoValidData(ofstream& outFile)
 {
   outFile << endl;
   outFile << "There no valid data." << endl << endl;
   outFile << "*< end >*" << endl;
 }
 void printStat(ofstream& outFile, StuData student)
 {
   student.perFe = (float(student.totFe) / float (student.exams.totA + student.exams.totB + student.exams.totC + student.exams.totD + student.exams.totF)) * 100;
   student.perMa = (float(student.totMa) / float (student.exams.totA + student.exams.totB + student.exams.totC + student.exams.totD + student.exams.totF)) * 100;


   outFile << endl;
   outFile << ">>Total A: " << student.exams.totA << endl;
   outFile << ">>Total B: " << student.exams.totB << endl;
   outFile << ">>Total C: " << student.exams.totC << endl;
   outFile << ">>Total D: " << student.exams.totD << endl;
   outFile << ">>Total F: " << student.exams.totF << endl << endl;

   outFile << ">> " << "Max of all exams: " << student.exams.highest << endl;
   outFile << ">> " << "Min of all exams: " << student.exams.lowest << endl << endl;
   outFile << ">> " << "Percent of Females: % " << student.perFe << endl;
   outFile << ">> " << "Percent of Males: % " << student.perMa << endl << endl;
   outFile << "*< end >*" << endl << endl;
 }



This is a long program and it's hard for me to tell you exactly where with this, but creating a struct array is pretty simple. From glancing at your code, it looks like your teacher wants you to make a student array. The line StuData myStudents[100]; will make such an array that can hold 100 different students. You can access each object within the array almost exactly the same you would with any other object or array. myStudents[0].exams.examAvg; will give you the average exam grade of the first student in the array.

The purpose behind this is to prevent the need have to read from a file just to get your information. I hope this will help you get on the right path.
Thank you very much this actually helped a lot and im kicking myself for it being so simple my biggest problem was i was over complicating the whole thing.
Welcome to programming 101, if it seems really simple, it probably is. If it seems overly complicated, it's probably because you made it that way.
yea i also made a mess of it the first time around i went back and pretty much rewrote it, one problem im facing now is how do i can i reprint the the data so that i can sort it.

Here is the main():
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

// New Struct StuData
*******************
struct StuData
{
    string name;
	int id;
	char sex;
	float e1;
	float e2;
	float e3;
	int examAvg;
	gradeType stuGrade;
};
//*********************
int main()
{
    ifstream inFile;
    ofstream outFile;

    inFile.open("in.data");
    outFile.open("out.data");

    outFile.setf(ios::fixed);
    outFile.setf(ios::showpoint);
    outFile.precision(2);

    printHead(outFile);
    readFile(inFile);

    stats.totA = 0;
    stats.totB = 0;
    stats.totC = 0;
    stats.totD = 0;
    stats.totF = 0;
    stats.highest = -1;
    stats.lowest = -1;
    stats.totMa = 0;
    stats.totFe = 0;
    stats.perFe = 0;
    stats.perMa = 0;
    stats.invalid = 0;

    while(inFile)
    {
        student[0].sex = sexUpper(student);
        sexCount(student);
        printData(outFile, student);

        if(validData(student))
        {

            student[0].examAvg = avg(student);
            stats.mean = stats.mean + student[0].examAvg;
            student[0].stuGrade = grade(student);

            examHighLow(student);
            printAvgAndGrade(outFile, student);

        }
        else
            printInvalidData(outFile);

        readFile(inFile);
    }

    if (stats.highest + stats.lowest == -2)
        printNoValidData(outFile);

    else

    printStat(outFile, stats);

}
Topic archived. No new replies allowed.