I cannot figure out how to complete this nested loop with the appropriate output ">>>Average of all quizzes: 19.10" I keep getting 18.50. The input I have is this:
1111 3 20 18 19
7821 2 18 20
9102 4 20 20 20 20
8888 1 16
After the 3,2,4,1 are the quizzes (10 of them). Can somebody help me with my code to get the correct value ">>>Average of all quizzes: 19.10", instead of ">>>Average of all quizzes: 18.50"?? I know I need to divide the total of all quizzes by the number of quizzes, but I'm not sure how to do this correctly. Please help!
Here is the code that I have:
Code:#include <iostream> #include <fstream> #include <iomanip> using namespace std; int main() { ifstream inFile; ofstream outFile; inFile.open("in.data"); outFile.open("out.data"); if(inFile.fail()||outFile.fail()) { cout << "Check your files" << endl; return 1; } outFile.setf(ios::fixed); outFile.precision(2); int studId; int numOfQuiz; int examlow = 0; int quizScore = 0; int records = 0; int lowStudId; int highStudId; int totNumQuiz; float totAvg = 0; float quizAvgHigh = 0; float quizAvgLow = 20; float allAvg = 0; float avg = 0; float quizTotAllStud = 0; float quizTotSingStud = 0; inFile >> studId >> numOfQuiz; outFile << "*~~< Stud Quiz Report >~~*" << endl << endl; outFile << setw(20) << left << "Stud Id" << left << setw(20) << left << "AVG of Quizzes" << endl; outFile << setw(20) << left << "-------" << left << setw(20) << left << "--------------" << endl; while(inFile) { for(int i = 0; i < numOfQuiz; i++) { inFile >> quizScore; totNumQuiz++; quizTotAllStud += quizScore; quizTotSingStud += quizScore; } avg = (quizTotAllStud/numOfQuiz); totAvg = totAvg + avg; outFile << setw(20) << studId << avg << endl; quizTotAllStud = 0; if(avg >= quizAvgHigh) { quizAvgHigh = avg; highStudId = studId; } if(avg <= quizAvgLow) { quizAvgLow = avg; lowStudId = studId; } records++; quizTotSingStud = 0; inFile >> studId >> numOfQuiz; } outFile << endl << ">>>Average of all quizzes: " << (totNumQuiz/numOfQuiz) << endl << endl; outFile << ">>>Student with id " << highStudId << " has highest quiz average of " << quizAvgHigh << "" << endl; outFile << ">>>Student with id " << lowStudId << " has lowest quiz average of " << quizAvgLow << "" << endl << endl; outFile << ">>>There were " << records << " students & " << totNumQuiz << " quizzes in file." << endl; inFile.close(); outFile.close(); return 0; }



1Likes
LinkBack URL
About LinkBacks




...