// File: gradbook.cpp. Method definitions for GradeBook class #include "gradbook.h" // // Constructor to obtain number of grades and possible points // for tests // Pre: none // Post: NumGrades is the number of grades. // MaxTotalPoints is the number of possible points. // GradeBook::GradeBook() { ReadNumGrades(); GetMaxTotalPoints(); } // // Method to read number of grades // Pre: The GradeBook object has been created. // Post: The (positive) number of grades was obtained. // void GradeBook::ReadNumGrades(void) { cout << "How many grades are there? "; cin >> NumGrades; while (NumGrades <= 0) { cout << "The number of grades must be positive.\n"; cout << "How many grades are there? "; cin >> NumGrades; } } // // Method to obtain the maximum total number of points // possible for a student to earn // Pre: The GradeBook object has been created. // Post: The (positive) maximum possible points was obtained. // void GradeBook::GetMaxTotalPoints(void) { cout << "\nEnter the value of each grade.\n"; MaxTotalPoints = ReadAddGrades(); while (MaxTotalPoints <= 0) { cout << "The grade total must be positive.\n"; cout << "Please enter grades again.\n\n"; MaxTotalPoints = ReadAddGrades(); } } // // Function to read and total grades and to return total // Pre: The GradeBook object has been created. // Post: The nonnegative total grade points was returned. // int GradeBook::ReadAddGrades(void) { int TotalPoints; // total number of points TotalPoints = 0; for (int grade = 1; grade <= NumGrades; grade++) TotalPoints += ReadGrade(grade); return TotalPoints; } // // Function to read and return an integer grade // Pre: grade is the number of the grade. // Post: A nonnegative grade was returned. // int GradeBook::ReadGrade(int grade) { int points; // points for one test do { cout << "Enter the nonnegative value of grade " << grade << ": "; cin >> points; } while (points < 0); return points; } // // Function to compute and return the average of a student // Pre: NumGrades is the (positive) number of grades. // MaxTotalPoints is the (positive) total worth of grades. // student is the student number // Post: A student's average was returned. // float GradeBook::GetAverage(int student) { int TotalPoints; // total number of points float average; // student's average cout << "\nEnter the grades for student " << student << ".\n"; TotalPoints = ReadAddGrades(); average = 100.0 * TotalPoints / MaxTotalPoints; return average; }