simple C++ // Fig. 5.11: fig05_11.cpp // Creating a GradeBook object and calling
ID: 3665828 • Letter: S
Question
simple C++
// Fig. 5.11: fig05_11.cpp
// Creating a GradeBook object and calling its member functions.
#include "GradeBook.h" // include definition of class GradeBook
int main()
{
// create GradeBook object
GradeBook myGradeBook( "CS101 C++ Programming" );
myGradeBook.displayMessage(); // display welcome message
myGradeBook.inputGrades(); // read grades from user
myGradeBook.displayGradeReport(); // display report based on grades
} // end main
/**************************************************************************
* (C) Copyright 1992-2014 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
**************************************************************************/
// Fig. 5.10: GradeBook.cpp
// Member-function definitions for class GradeBook that
// uses a switch statement to count A, B, C, D and F grades.
#include <iostream>
#include "GradeBook.h" // include definition of class GradeBook
using namespace std;
// constructor initializes courseName with string supplied as argument;
// initializes counter data members to 0
GradeBook::GradeBook( string name )
: aCount( 0 ), // initialize count of A grades to 0
bCount( 0 ), // initialize count of B grades to 0
cCount( 0 ), // initialize count of C grades to 0
dCount( 0 ), // initialize count of D grades to 0
fCount( 0 ) // initialize count of F grades to 0
{
setCourseName( name );
} // end GradeBook constructor
// function to set the course name; limits name to 25 or fewer characters
void GradeBook::setCourseName( string name )
{
if ( name.size() <= 25 ) // if name has 25 or fewer characters
courseName = name; // store the course name in the object
else // if name is longer than 25 characters
{ // set courseName to first 25 characters of parameter name
courseName = name.substr( 0, 25 ); // select first 25 characters
cerr << "Name "" << name << "" exceeds maximum length (25). "
<< "Limiting courseName to first 25 characters. " << endl;
} // end if...else
} // end function setCourseName
// function to retrieve the course name
string GradeBook::getCourseName() const
{
return courseName;
} // end function getCourseName
// display a welcome message to the GradeBook user
void GradeBook::displayMessage() const
{
// this statement calls getCourseName to get the
// name of the course this GradeBook represents
cout << "Welcome to the grade book for " << getCourseName() << "! "
<< endl;
} // end function displayMessage
// input arbitrary number of grades from user; update grade counter
void GradeBook::inputGrades()
{
int grade; // grade entered by user
cout << "Enter the letter grades." << endl
<< "Enter the EOF character to end input." << endl;
// loop until user types end-of-file key sequence
while ( ( grade = cin.get() ) != EOF )
{
// determine which grade was entered
switch ( grade ) // switch statement nested in while
{
case 'A': // grade was uppercase A
case 'a': // or lowercase a
++aCount; // increment aCount
break; // necessary to exit switch
case 'B': // grade was uppercase B
case 'b': // or lowercase b
++bCount; // increment bCount
break; // exit switch
case 'C': // grade was uppercase C
case 'c': // or lowercase c
++cCount; // increment cCount
break; // exit switch
case 'D': // grade was uppercase D
case 'd': // or lowercase d
++dCount; // increment dCount
break; // exit switch
case 'F': // grade was uppercase F
case 'f': // or lowercase f
++fCount; // increment fCount
break; // exit switch
case ' ': // ignore newlines,
case ' ': // tabs,
case ' ': // and spaces in input
break; // exit switch
default: // catch all other characters
cout << "Incorrect letter grade entered."
<< " Enter a new grade." << endl;
break; // optional; will exit switch anyway
} // end switch
} // end while
} // end function inputGrades
// display a report based on the grades entered by user
void GradeBook::displayGradeReport() const
{
// output summary of results
cout << " Number of students who received each letter grade:"
<< " A: " << aCount // display number of A grades
<< " B: " << bCount // display number of B grades
<< " C: " << cCount // display number of C grades
<< " D: " << dCount // display number of D grades
<< " F: " << fCount // display number of F grades
<< endl;
} // end function displayGradeReport
/**************************************************************************
* (C) Copyright 1992-2014 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
**************************************************************************/
Explanation / Answer
I have written the gradebook.cpp code with comments taken from your code given above. I have completed the function display grade report as well.
Working code:
#include <iostream>
#include <iomanip>
#include "GradeBook.h"
using namespace std;
GradeBook::GradeBook( string name )
{
cout <<"The Grade Book Constructor is called"<<endl; //Cout when calling Constructor
setCourseName( name ); // validate and store courseName
aCount = 0; // initialize count of A grades to 0
bCount = 0; // initialize count of B grades to 0
cCount = 0; // initialize count of C grades to 0
dCount = 0; // initialize count of D grades to 0
fCount = 0; // initialize count of F grades to 0
displayMessage();
cout <<"The Grade Book, " << getCourseName() <<", Contains" <<endl<<endl <<endl;
displayGradeReport(0);
cout <<"****The End Grade Book Constructor.****" <<endl; //cout at end of Constructor
} // end GradeBook constructor
GradeBook::~GradeBook() //call Decontructor
{
string name;
name =""; // set name variable
cout <<"*****Destructor is called***** "<<endl;
setCourseName( name ); // clear name
} // end GradeBook Destructor
// function to set the course name; limits name to 25 or fewer characters
void GradeBook::setCourseName( string name )
{
if ( name.length() <= 30 ) // if name has 30 or fewer characters
courseName = name; // store the course name in the object
else // if name is longer than 30 characters
{ // set courseName to first 30 characters of parameter name
courseName = name.substr( 0, 30 ); // select first 30 characters
cout << setw( 10 ) << " "<< "Name "" << name<< """ << endl;
cout << setw( 10 ) << " "<< "exceeds maximum length (30)." << endl<<endl<<endl;
cout << setw( 10 ) << " "<< "Limiting courseName to first 30 characters. " << endl;
} // end if...else
} // end function setCourseName
// function to retrieve the course name
string GradeBook::getCourseName()
{
return courseName;
} // end function getCourseName
// display a welcome message to the GradeBook user
void GradeBook::displayMessage()
{
// this statement calls getCourseName to get the
// name of the course this GradeBook represents
cout << setw( 10 ) << " " <<"Welcome to the grade book for"<< endl; //indent cout
cout << setw( 10 ) << " " << getCourseName() << "!" << endl << endl;//indent cout
} // end function displayMessage
// input arbitrary number of grades from user; update grade counter
void GradeBook::inputGrades()
{
int grade; // grade entered by user
cout << setw( 10 ) << " " <<"Enter the letter grades." << endl;
cout << setw( 10 ) << " " << "Enter the EOF character to end input." << endl;
cout << setw( 10 ) << " " <<"(Use Ctl + D, or Ctl + Z)"<<endl;
// loop until user types end-of-file key sequence
while ( ( grade = cin.get() ) != EOF )
{
// determine which grade was input
switch ( grade ) // switch statement nested in while
{
case 'A': // grade was uppercase A
case 'a': // or lowercase a
aCount++; // increment aCount
break; // exit switch
case 'B': // grade was uppercase B
case 'b': // or lowercase b
bCount++; // increment bCount
break; // exit switch
case 'C': // grade was uppercase C
case 'c': // or lowercase c
cCount++; // increment cCount
break; // exit switch
case 'D': // grade was uppercase D
case 'd': // or lowercase d
dCount++; // increment dCount
break; // exit switch
case 'F': // grade was uppercase F
case 'f': // or lowercase f
fCount++; // increment fCount
break; // exit switch
case ' ': // ignore newlines,
case ' ': // tabs,
case ' ': // and spaces in input
break; // exit switch
default: // catch all other characters
cout << setw( 10 ) << " " << "****Incorrect letter grade entered.****"<<endl;
cout << setw( 10 ) << " " << " Enter a new grade." << endl;
break; // optional; will exit switch anyway
} // end switch
} // end while
} // end function inputGrades
// display a report based on the grades entered by user
void GradeBook::displayGradeReport()
{
// display summary of results
// calculate total grades
int gradeCount = aCount + bCount + cCount + dCount + fCount;
cout << " The total number of students receive grades is "<< gradeCount <<endl;
cout << "Number of students who received each letter grade:"
<< " A: " << aCount // display number of A grades
<< " B: " << bCount // display number of B grades
<< " C: " << cCount // display number of C grades
<< " D: " << dCount // display number of D grades
<< " F: " << fCount // display number of F grades
<< endl <<endl;
// calculate total grades
// display class average
// if user entered at least one grade
if ( gradeCount != 0 )
{
// calculate total grades
int gradeTotal = 4 * aCount + 3 * bCount + 2 * cCount + 1 * dCount;
// set floating-point number format
cout << fixed << setprecision( 1 );
// compute and display class GPA with 1 digit of precision
cout << " The class average is: "
<< static_cast< double > ( gradeTotal ) / gradeCount
<< endl<<endl <<endl;
} // end if
} // end function displayGradeReport
void GradeBook::displayGradeReport(int n)
{
// display summary of results
// calculate total grades
int gradeCount = aCount = bCount = cCount = dCount = fCount =n;
cout << "The total number of students receive grades is "<< gradeCount <<endl;
cout << "Number of students who received each letter grade:"
<< " A: " << aCount // display number of A grades
<< " B: " << bCount // display number of B grades
<< " C: " << cCount // display number of C grades
<< " D: " << dCount // display number of D grades
<< " F: " << fCount // display number of F grades
<< endl <<endl;
// calculate total grades
// display class average
// calculate total grades
int gradeTotal = 4 * aCount + 3 * bCount + 2 * cCount + 1 * dCount;
// set floating-point number format
cout << fixed << setprecision( 1 );
// compute and display class GPA with 1 digit of precision
if ( gradeCount != 0 )
{
cout << " The class average is: "
<< static_cast< double > ( gradeTotal ) / gradeCount
<< endl << endl <<endl;
}
else
{
cout << " The class average is: 0.0"<<endl<<endl<<endl;
}
} // end function displayGradeReport
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.