making struct display chronologically
May 2, 2012 at 3:52pm UTC
So I have a struct that intputs a external file and displays values. I'm trying to find a way to display each student in order from highest average grades to lowest.Can anyone help? I will show the external file and the struct I made so far.
1 2 3 4 5 6 7 8 9 10
3313 90 42 58 64 70 75 100
5688 88 48 79 70 79 70 94
4700 50 44 89 73 70 73 100
9561 88 69 88 87 84 63 98
3199 96 69 100 90 88 67 100
3768 78 57 80 59 57 15 60
8291 72 56 70 82 74 9 83
7754 76 62 93 100 78 41 58
8146 94 68 99 94 93 9 54
2106 98 47 96 94 70 27 100
And here is the 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 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
// File: stugrades.cpp
// Computes a external file and compiles it into a table with 3 columns of ID numbers, scores, and corresponding scores.
#include <iostream>
#include <fstream> // re. ifstream object
#include <iomanip> // re. setw, setprecision, fixed in formatted output
using namespace std;
struct examStats
{
string id;
int scores;
string grade;
float average;
float sum;
};
int main()
{
ifstream fin( "grades.txt" ); // construct ifstream object fin
if ( fin ) // if opened ok ...
{
string id;
string grade;
int count = 7;
float average;
string student;
int mean = 72.8;
examStats student_data[10];
while ( fin >> id ) //removed a >
{
int sum = 0, score[7];
//int mean = 0, average[7];
for ( int i = 0; i < 7; ++ i )
{
fin >> score[i]; // removed a >
sum += score[i];
average = (sum / count);
student_data[0].id = id;
student_data[0].scores = score[0];
student_data[1].scores = score[1];
student_data[2].scores = score[2];
student_data[3].scores = score[3];
student_data[4].scores = score[4];
student_data[5].scores = score[5];
student_data[6].scores = score[6];
student_data[0].grade = grade;
student_data[0].average = average;
student_data[0].sum = sum;
if (average > mean + 10)
{
grade = "outstanding" ;
}
else if (average < mean - 10)
{
grade = "unsatisfactory" ;
}
else if ((average <= mean + 10) && (average >= mean - 10))
{
grade = "satisfactory" ;
}
}
cout << student_data[0].id << endl;
cout << student_data[0].scores << " " << student_data[1].scores << " " << student_data[2].scores << " " << student_data[3].scores << " " << student_data[4].scores << " " << student_data[5].scores<< " " << student_data[6].scores << endl;
cout << student_data[0].average << endl;
cout << student_data[0].grade << endl;
cout << student_data[0].sum << endl;
cout << endl;
}
fin.close(); // closes file
}
}
Topic archived. No new replies allowed.