### course score calculation program

The program I am trying to get to work takes in a string student ID. 2 double test scores (max score of 200) and 7 double home work scores(max of 265) from a text file. It adds the scores up for test and homework separately then determines an average for them both separately. Then determines a final grade based on the tests counting for 60% of the total grade and the homework is the other 40%.

The problem is the scores are not calculating correctly from the first one on. The math works on paper but not in the program. And after each calculation the previous wrong score adds to the next students data. I think I need to add another loop but I am not sure? Any help would be greatly appreciated!!!!!!

input from the file prg5Data.txt

1
123456789 77.2 88.3 22 28 35 45 33 35 40
2
234567890 97.5 90 25 30 38 48 34 35 50
3
345678901 82.4 77.5 22.5 27 35.5 44 35 33 48

The first calculations are wrong, and it continues summing scores. What the correct score should be is in().

STUDENT ID / HW AVG / TEST AVG / FINAL SCORE / GRADE

123456789 / 98.1(89.8) / 80.0(82.8) / 87.2(85.6) / B
234567890 / 196.2 / 159.9 / 174.4 / A
345678901 / 294.3 / 239.9 /261.6 / A

Number of A's: 2
Number of B's: 1

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170`` `````` #include #include #include #include #include using namespace std; const int CLASS_SIZE = 30; const int AMOUNT_OF_TESTS = 2; const int AMOUNT_OF_HOMEWORK = 7; const int MAX_STUDENT_ID_LENGTH = 9; const double PERFECT_HOMEWORK_SCORE = 265; const double PERFECT_TEST_SCORE = 200; struct StudentType { string studentID; double testGrades, homeWorkGrades, hwAve, testAve, finalScore; char finalGrade; }; int getInput(StudentType[], int); void process(StudentType[], int); void display(const StudentType[], int); int main() { int totStudents; StudentType gradeData[CLASS_SIZE]; totStudents = getInput(gradeData, CLASS_SIZE); process(gradeData, totStudents); display(gradeData, totStudents); _getch(); return 0; } int getInput(StudentType gradeData[], int classSize) { int totalStudents = 0; int totalTests = 0; int totalHomeWork = 0; ifstream ifile; ifile.open("prg5data.txt"); if(ifile.fail()) { cout<< "Can't find file prg5data.txt" << endl << endl; _getch(); return 0; } for(totalStudents = 0; totalStudents < classSize; totalStudents++) { ifile >> gradeData[totalStudents].studentID; if(gradeData[totalStudents].studentID.length() <= MAX_STUDENT_ID_LENGTH) { for(totalTests = 0; totalTests < AMOUNT_OF_TESTS; totalTests++) ifile >> gradeData[totalTests].testGrades; for(totalHomeWork = 0; totalHomeWork < AMOUNT_OF_HOMEWORK; totalHomeWork++) ifile >> gradeData[totalHomeWork].homeWorkGrades; } else { cout << "StudentID: " << gradeData[totalStudents].studentID << " can't be greater than 9 integers. "; _getch(); return 0; } if(!ifile) break; } ifile.close(); return totalStudents; } void process(StudentType gradeData[], int totStudents) { int processIndex = 0; int testAvgIndex = 0; int homeWorkAvgIndex = 0; double testScoreSum = 0; double homeWorkSum = 0; for (processIndex = 0; processIndex < totStudents; processIndex++) { for(testAvgIndex = 0; testAvgIndex < AMOUNT_OF_TESTS; testAvgIndex++) { testScoreSum += gradeData[testAvgIndex].testGrades; } for(homeWorkAvgIndex = 0; homeWorkAvgIndex < AMOUNT_OF_HOMEWORK; homeWorkAvgIndex++) { homeWorkSum += gradeData[homeWorkAvgIndex].homeWorkGrades; } gradeData[processIndex].testAve = ((testScoreSum / PERFECT_TEST_SCORE) * 100); gradeData[processIndex].hwAve = ((homeWorkSum / PERFECT_HOMEWORK_SCORE) * 100); gradeData[processIndex].finalScore = (gradeData[processIndex].testAve * .6) + (gradeData[processIndex].hwAve * .4); if (gradeData[processIndex].finalScore > 90) gradeData[processIndex].finalGrade = 'A'; else if (gradeData[processIndex].finalScore > 80 && gradeData[processIndex].finalScore < 89.9) gradeData[processIndex].finalGrade = 'B'; else if (gradeData[processIndex].finalScore > 70 && gradeData[processIndex].finalScore < 79.9) gradeData[processIndex].finalGrade = 'C'; else if (gradeData[processIndex].finalScore > 60 && gradeData[processIndex].finalScore < 69.9) gradeData[processIndex].finalGrade = 'D'; else gradeData[processIndex].finalGrade = 'E'; } } void display (const StudentType gradeData[], int totStudents) { ofstream outFile; outFile.open("prg6out.txt"); int displayIndex = 0; int totA = 0; int totB = 0; int totC = 0; int totD = 0; int totE = 0; outFile << "STUDENT ID" << setw(15) << "HW AVE" << setw(15) << "TEST AVE" << setw(15) << "FINAL SCORE" << setw(15) << "GRADE" << endl; outFile << endl; for(displayIndex = 0; displayIndex < totStudents; displayIndex++) { outFile << fixed << showpoint << setprecision(1); outFile << gradeData[displayIndex].studentID << setw(15) << gradeData[displayIndex].hwAve << setw(15) << gradeData[displayIndex].testAve << setw(15) << gradeData[displayIndex].finalScore << setw(15) << gradeData[displayIndex].finalGrade << endl; if (gradeData[displayIndex].finalGrade == 'A') totA++; else if (gradeData[displayIndex].finalGrade == 'B') totB++; else if (gradeData[displayIndex].finalGrade == 'C') totC++; else if (gradeData[displayIndex].finalGrade == 'D') totD++; else totE++; } outFile << endl; outFile << "Number of A's: " << setw(10) << totA << endl << "Number of B's: " << setw(10) << totB << endl << "Number of C's: " << setw(10) << totC << endl << "Number of D's: " << setw(10) << totD << endl << "Number of E's: " << setw(10) << totE << endl; outFile.close(); } ``````
Last edited on
edited to make it easier to read
Topic archived. No new replies allowed.