I'm attempting to do the following problem:
Write a program for the following problem. You’re given a file that contains a collection if IDs and scores (type int) for an exam in your computer course. You’re to compute the average of these scores and assign grades to each student according to the following rule:
If a student’s score is within 10 points (above or below) of the average, assign a grade of satisfactory. If a studnt’s score is more than 10 points above average, assign a grade of outstanding. If a student’s score is more than 10 points below average, assign a grade of unsatisfactory.
The output from your program should consist of a labeled three-column list that shows each ID, score, and corresponding grade. Use a struct to store each student’s data and an array of structs to store the whole class. The struct should have a data member for id, score, and grade.
The file given to us 'grades' is as follows:
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
So far, I have this code:
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
|
#include <fstream> //required for file streams
#include <iostream> //input/output
#include <cstdlib> //for definition of EXIT_FAILURE
#include <string>
#include <iomanip>
using namespace std;
#define inFileGrades "grades.txt" //grades
#define outFile "gradesOut.txt" //grade sheet
//Functions used
void processGrades (ifstream&, ofstream&, int&, int&); //process all
void classAverage (double&, double&, string&);
int main()
{
ifstream grades; //input: grades
ofstream gradesOut; //output: all info per employee
int studentSum; //sum value
int totalSum; //totalSum
studentSum = 0; //initiate sum
totalSum = 0; //initiate total sum
//Prepare files
grades.open(inFileGrades); //open grade file
if (grades.fail()) //if file doesn't read in, fail message
{
cerr << "*** ERROR: Cannot open " << inFileGrades << " for input." << "\n";
return EXIT_FAILURE; //failure return
}
gradesOut.open(outFile); //make output file
if (gradesOut.fail()) //if file isn't made, fail message
{
cerr << "*** ERROR: Cannot open " << gradesOut << " for output." << "\n";
return EXIT_FAILURE; //failure return
}
processGrades(grades, //process grade file to form output file
gradesOut,
studentSum,
totalSum);
//Close files
grades.close();
gradesOut.close();
return 0;
}
void processGrades(ifstream& grades,
ofstream& gradesOut,
int& studentSum,
int& totalSum)
{
const int MAX_IDS = 10;
const int MAX_SCORE = 7;
int id[MAX_IDS]; //student id
int score[MAX_SCORE]; //first score
double studentAverage[MAX_IDS]; //average value
double totalAverage;
string third[MAX_IDS];
for (int j = 0;
j < MAX_IDS;
j++)
{
for (int i = 0;
i < MAX_SCORE;
i++)
{
grades >> id[j] >> score[i];
studentSum += score[i];
studentAverage[j] = studentSum / MAX_SCORE;
totalSum = totalSum + studentSum;
}
studentSum = 0;
}
totalAverage = totalSum / MAX_IDS;
classAverage (studentAverage,
totalAverage,
third);
for (int j = 0;
j < MAX_IDS;
j++)
{
gradesOut << setw(4) << id[j] << setw(8) << studentAverage[j] << setw(14) << third[j];
}
}
void classAverage (double& studentAverage,
double& totalAverage,
string& third)
{
if (studentAverage < totalAverage - 10)
{
third == "Unsatisfactory";
}
else if (studentAverage > totalAverage + 10)
{
third == "Oustanding";
}
else
{
third == "Satisfactory";
}
}
|
I'm not sure how close I am, because it will not compile do to this error.
./grader.cpp: In function `void processGrades(std::ifstream&, std::ofstream&, int&, int&)':
./grader.cpp:97: error: invalid initialization of non-const reference of type 'double&' from a temporary of type 'double*'
./grader.cpp:19: error: in passing argument 1 of `void classAverage(double&, double&, std::string&)'
I appreciate any advice.