### Need help displaying an average for each entry

I have to display an average for each student that is entered can someone please help with fixing my code
 ``1234567891011121314151617181920212223`` ``````void StudentData::displayAverageScore() { double totalScore = 0; double totalEx1 = 0, totalEx2 = 0, totalEx3 = 0, averageScore = 0; for(int i = 0; i < entries.size(); i++) { totalEx1 += entries[i].getEx1();// accumulate totals totalEx2 += entries[i].getEx2(); totalEx3 += entries[i].getEx3(); if(totalScore != 0) averageScore = (totalEx1 + totalEx2 + totalEx3) / 3; // don't divide by 0 cout << "Average Score For Exams: " << setprecision(1) << fixed << averageScore << endl << endl; } }``````
In this code, totalscore is always 0 therefore the if statemenf will never be executed.

Aceix.
This ^
USe not accumulator, but current entry data: `averageScore = (entries[i].getEx1() + /*...*/) / 3;`
Thanks for the help getting the average to work , but I still need help getting it to average for each student entered. If the full code would help I can post it
Why not.

Aceix.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349`` ``````#include #include #include #include #include using namespace std; class Student { // keep this class simple and to a minimum public: Student(string firstN, string lastN, double ex1, double ex2, double ex3); string getFirstN(); string getLastN(); double getEx1(); double getEx2(); double getEx3(); private: string firstN; string lastN; double ex1; double ex2; double ex3; }; Student::Student(string fn, string ln, double e1, double e2, double e3) { firstN = fn; lastN = ln; ex1 = e1; ex2 = e2; ex3 = e3; } string Student::getFirstN() { return firstN; } string Student::getLastN() { return lastN; } double Student::getEx1() { return ex1; } double Student::getEx2() { return ex2; } double Student::getEx3() { return ex3; } class StudentData { public: StudentData(); void loadDataFromFile(); void addStudent(); void deleteStudent(); void displayStudentData(); void displayAverageScore(); void saveAndExit(); private: vector entries;// vector of Student objects }; StudentData::StudentData() {} // default constructor void StudentData::loadDataFromFile() { string firstN, lastN; double ex1, ex2, ex3; ifstream studentData; studentData.open("studentData.txt"); // open the file if(studentData.is_open()) { while( studentData >> firstN >> lastN >> ex1 >> ex2 >> ex3 ) { Student student(firstN, lastN, ex1, ex2, ex3); // create aStudent object entries.push_back(student); } } cout << entries.size() << " Total students entered "<< endl << endl; studentData.close(); } void StudentData::addStudent() { string fn, ln; double s1, s2, s3; char ans; do { cout << " Enter First Name: "; cin >> fn; cout << " Enter Last Name: "; cin >> ln; cout << " Enter Score 1: "; cin >> s1; cout << " Enter Score 2: "; cin >> s2; cout << " Enter Score 3: "; cin >> s3; Student temp(fn,ln,s1,s2,s3); entries.push_back(temp); cout << " Would you like to enter another record? Y or N : "; cin >> ans; ans = tolower(ans); cout << endl << endl; }while(ans == 'y'); } void StudentData::deleteStudent() { int index = 0; cout << "There are " << entries.size() << " records at this time. " ; if(entries.size() == 0) { cout << " No records can be removed. " << endl << endl; return; } else { cout << "The names are: " << endl << endl; for(int i = 0; i < entries.size(); i++) cout << i + 1 << '\t' << entries[i].getFirstN() << " " << entries[i].getLastN() << endl; cout << endl << " Enter the number of the file that you wish to delete: 1 through " << entries.size(); cin >> index; if(index <= 0 || index > entries.size()) { cout << " The number you entered is not within the proper range." << endl; } index = index - 1; for(int i = index; i < entries.size() - 1; i++) entries[i] = entries[i+1]; entries.pop_back(); } cout << " There are now " << entries.size() << " records." << endl; } void StudentData::displayStudentData(){ cout << " There are " << entries.size() << " at this time. " << endl << endl; if(entries.size() > 0) { cout << " RecNum\tFName\tLName\tScore1\tScore2\tScore3\t" << endl; for(int i = 0; i < entries.size(); i++) cout << " " << (i + 1) << "." << '\t' << entries[i].getFirstN() << '\t' << entries[i].getLastN() << '\t' << entries[i].getEx1() << '\t' << entries[i].getEx2() << '\t' << entries[i].getEx3() << endl; } cout << endl; } void StudentData::displayAverageScore() { double totalScore = 0; double getEx1 = 0, getEx2 = 0, getEx3 = 0, averageScore = 0; for(int i = 0; i < entries.size(); i++) { getEx1 += entries[i].getEx1();// accumulate totals getEx2 += entries[i].getEx2(); getEx3 += entries[i].getEx3(); if(averageScore = (getEx1 + getEx2 + getEx3) / 3); // don't divide by 0 cout << "Average Score For Exams: " << setprecision(1) << fixed << averageScore << endl << endl; } } void StudentData::saveAndExit() { ofstream outFile; outFile.open("studentData.txt"); if(outFile.is_open()) { for(int i = 0; i < entries.size(); i++) outFile << entries[i].getFirstN() << '\t' << entries[i].getLastN() << '\t' << entries[i].getEx1() << '\t' << entries[i].getEx2() << '\t' << entries[i].getEx3() << endl; cout << entries.size() << " The records were saved to studentData.txt" << endl << endl; } } int main() { // test program StudentData entries; // int option; do { cout << "----------Menu Options--------------" << endl; cout << " 1. Load a students records " << endl; cout << " 2. Enter students name and scores " << endl; cout << " 3. Display students information " << endl; cout << " 4. Remove a students information " << endl; cout << " 5. Display Exam Averages " << endl; cout << " 6. Save and Exit " << endl; cout << " Enter your choice 1, 2, 3, 4 ,5 " << endl; cin >> option; cin.clear(); cin.ignore(50, '\n'); cout << endl; switch(option) { case 1: entries.loadDataFromFile(); // add an entry object to the log break; case 2: entries.addStudent(); // add an entry object to the log break; case 3: entries.displayStudentData(); // display all records break; case 4: entries.deleteStudent(); break; case 5: entries.displayAverageScore(); // display average MPG break; case 6: entries.saveAndExit(); // save to file and exit program break; default: cout << "Please enter 1, 2, 3, 4, 5, or 6 : " << endl << endl; } } while(option!= 6); cout << endl; return 0; }``````
These are the direction
 1.a loadStudents method that loads a set of student records from a text file into a Student objects and adds each student object to the course vector. Record structure: lName fName ex1 ex1 ex3, separated by spaces or tabs. 2.an addStudent method to add a Student to the Course vector. 3.a displayStudents method that prints a list of all the students names and scores 4.a removeStudent method that removes a Student from the Course vector. 5.a displayAverages method that displays all of the Student's names and their exam averages. 6.a storeData method that stores each student record to a text file and exits the program
Topic archived. No new replies allowed.