(GradeBook Modification) Modify the GradeBook program 5.11 to calculate the grade-point average. A grade of A is worth 4 points, B is worth 3 points, and so on.
Solution:
Solution:
#include <string> using std::string; //GradeBook class definition class GradeBook { public: GradeBook( string ); // course name initialization with constructor void setCourseName( string ); // course name set function string getCourseName(); //course name get function void displayMessage(); //display greeting message void inputGrades(); //enter random grade count void displayGradeReport(); // display entered grades report private: string courseName; int aCount; int bCount; int cCount; int dCount; int fCount; }; #include "GradeBook.h"
int main() { GradeBook myGradeBook( "CS101 C++ Programming" ); myGradeBook.displayMessage(); myGradeBook.inputGrades(); myGradeBook.displayGradeReport(); //for pause system("PAUSE"); return 0; }
#include <iostream> using std::cout; using std::cin; using std::endl; #include "GradeBook.h" //constructor GradeBook::GradeBook( string name ) { setCourseName( name ); aCount = 0; bCount = 0; cCount = 0; dCount = 0; fCount = 0; } // course name limit void GradeBook::setCourseName( string name ) { if( name.length() <= 25 ) courseName = name; else { courseName = name.substr( 0, 25); cout << "Name \"" << name << "\" exceeds maximum length (25).\n" << "LImiting courseName to first 25 characters.\n" << endl; } } //function get course name string GradeBook::getCourseName() { return courseName; } //function display message void GradeBook::displayMessage() { cout << "Welcome to the grade book for\n" << getCourseName() << "!\n" << endl; } //input random count of grades void GradeBook::inputGrades() { int grade; cout << "Enter the letter grades." << "Enter th EOF character to end input." << endl; while ( ( grade = cin.get() ) != EOF ) { switch( grade ) { case 'A': case 'a': aCount++; break; case 'B': case 'b': bCount++; break; case 'C': case 'c': cCount++; break; case 'D': case 'd': dCount++; break; case 'F': case 'f': fCount++; break; case '\n': case '\t': case ' ': break; default: cout << "Incorrect letter grade entered." << " Enter a new grade." << endl; } } } //report void GradeBook::displayGradeReport() { cout << "\n\nNumber of students who received each letter grade:" << "\nA: " << aCount << "\nB: " << bCount << "\nC: " << cCount << "\nD: " << dCount << "\nF: " << fCount << endl; cout << "Average point is " << (aCount * 4 + bCount * 3 + cCount * 2 + dCount * 1 + fCount * 0)/(aCount + bCount + cCount + dCount + fCount) << endl; }
Thanks you :)
ReplyDelete