#include #include #include #define NUMTESTS 3 #define INFILE "studentTests.dat" #define OUTFILE "studentAvg.out" #define INFLAG 1 #define OUTFLAG 0 using namespace std; /**************************/ /* Function Prototypes */ /**************************/ // open inputfile void openFile(string filename, fstream &fs, int flags); // Get the student's SSN and test scores void getStuInfo(fstream &ins, string &ssn, float &test1, float &test2, float &test3); // Calculate the average of the three test scores float calcAvg(float score1, float score2, float score3); // Print student's SSN and test average void printStuInfo(fstream &outs, string ssn, float testAvg); int main() { float test1, test2, test3; float avg; string ssn; fstream ins; // input file stream declaraction fstream outs; // output file stream declaraction openFile(INFILE, ins, INFLAG); openFile(OUTFILE, outs, OUTFLAG); // read first line; handles empty file getStuInfo(ins, ssn, test1, test2, test3); // while not EOF, read data, calc avg, output avg for a student while (!ins.eof()) { avg = calcAvg(test1, test2, test3); printStuInfo(outs, ssn, avg); getStuInfo(ins, ssn, test1, test2, test3); } // end while !EOF // close files ins.close(); outs.close(); return 0; } // end main void openFile(string filename, fstream &fs, int flags) { if (flags == INFLAG) fs.open(filename.c_str(), ios::in); else fs.open(filename.c_str(), ios::out); if (fs.fail()) { cout << "Error opening input file " << filename << endl; exit(1); } // end if fail return; } void getStuInfo(fstream &ins, string &ssn, float &test1, float &test2, float &test3) { ins >> ssn; ins >> test1; ins >> test2; ins >> test3; return; } // end function getStuInfo float calcAvg(float score1, float score2, float score3) { float sum; float ave; sum = score1 + score2 + score3; ave = sum / NUMTESTS; return ave; } // end function calcAvg void printStuInfo(fstream &outs, string ssn, float testAvg) { outs << fixed << showpoint << setprecision(2); outs << ssn << " " << testAvg << endl; return; } // end function printStuInfo