Need a help please. I have a program calculating average of students grades:
I need to re-write this program in classes. I have header file:Code:#include <string> #include <iostream> #include <iomanip> #include <fstream> using namespace std; const int TOTAL_STUDENTS = 10; const int TOTAL_GRADES = 10; void readInput(fstream *file, string[], int grades[][TOTAL_GRADES]); void calcAvg(string names[], int grades[][TOTAL_GRADES], float average[]); void printData(string names[], int grades[][TOTAL_GRADES], float average[]); int main() { string names[TOTAL_STUDENTS]; int grades[TOTAL_STUDENTS][TOTAL_GRADES]; float average[TOTAL_STUDENTS]; fstream inputfile; string fileName; cout << "Hello there!" << endl; cout << "This is a program to calculate students' average test scores" << endl; cout << "-------------------------------------------------------------" << endl; cout << "Now, enter the file name: "; cin >> fileName; cout << endl; inputfile.open(fileName.c_str()); if(inputfile.is_open()) { readInput(&inputfile, names, grades); calcAvg(names, grades, average); printData(names, grades, average); } else { cout << "Please enter a correct file name or make sure file is in the same directory as the program." << endl; return -1; } inputfile.close(); return 0; } void readInput(fstream *file, string names[], int grades[][TOTAL_GRADES]) { for(int i = 0; i < TOTAL_STUDENTS; i++) { *file >> names[i]; for(int j = 0; j < TOTAL_GRADES; j++) { *file >> grades[i][j]; } } } void calcAvg(string names[], int grades[][TOTAL_GRADES], float average[]) { float sum; for(int i = 0; i < TOTAL_STUDENTS; i++) { sum = 0; for(int j = 0; j < TOTAL_GRADES; j++) { sum += grades[i][j]; } sum = (float)sum/TOTAL_GRADES; average[i] = sum; } } void printData(string names[], int grades[][TOTAL_GRADES], float average[]) { for(int i = 0 ; i < TOTAL_STUDENTS; i++) { cout << names[i] << " "; for(int j = 0; j < TOTAL_GRADES; j++) { cout << grades[i][j] << " "; } cout << fixed << showpoint << setprecision(2) << average[i] << endl; } }
I am OK with main program. But I don't know how the implementation file should look like. Any suggestions please?Code:#ifndef STUDENTTYPE_H #define STUDENTTYPE_H #include <iomanip> const int TOTAL_STUDENTS = 10; const int TOTAL_GRADES = 10; class StudentType { public: StudentType(); readInput(ifstream&):void; calcAvg():void; printData(ostream&) const:void; private: string names; int grades[TOTAL_GRADES]; float average; }; #endif // STUDENTTYPE_H
Thank youCode:#include "StudentType.h" #include <iostream> StudentType::StudentType() { name = ""; for (int i=0; i<TOTAL_STUDENTS; i++) { grades[i]={0}; } average = 0.0; } ??????



2Likes
LinkBack URL
About LinkBacks


