Implementing Sentinel-Controlled Repetition in Class GradeBook

Figures 4.94.10 show class GradeBook containing member function determineClassAverage (this class is demonstrated in Fig. 4.11). Although each grade entered is an integer, the averaging calculation is likely to produce a number with a decimal point—in other words, a real number or floating-point number (e.g., 7.33, 0.0975 or 1000.12345). C++ provides several data types for storing floating-point numbers in memory, including float and double. The primary difference between these types is that, compared to float variables, double variables can typically store numbers with larger magnitude and finer detail (i.e., more digits to the right of the decimal point—also known as the number’s precision). This program introduces a special operator called a cast operator to force the averaging calculation to produce a floating-point numeric result.


 1   // Fig. 4.9: GradeBook.h
 2   // Definition of class GradeBook that determines a class average.
 3   // Member functions are defined in GradeBook.cpp
 4   #include <string> // program uses C++ standard string class
 5
 6   // GradeBook class definition
 7   class GradeBook
 8   {
 9   public:
10      explicit GradeBook( std::string ); // initializes course name
11      void setCourseName( std::string ); // set the course name
12      std::string getCourseName() const; // retrieve the course name
13      void displayMessage() const; // display a welcome message
14      void determineClassAverage() const; // averages user-entered grades
15   private:
16      std::string courseName; // course name for this GradeBook
17   }; // end class GradeBook


Fig. 4.9. Class average using sentinel-controlled repetition: GradeBook header.


 1   // Fig. 4.10: GradeBook.cpp
 2   // Member-function definitions for class GradeBook that solves the
 3   // class average program with sentinel-controlled repetition.
 4   #include <iostream>
 5   #include <iomanip> // parameterized stream manipulators  
 6   #include "GradeBook.h" // include definition of class GradeBook
 7   using namespace std;
 8
 9   // constructor initializes courseName with string supplied as argument
10   GradeBook::GradeBook( string name )
11   {
12      setCourseName( name ); // validate and store courseName
13   } // end GradeBook constructor
14
15   // function to set the course name;
16   // ensures that the course name has at most 25 characters
17   void GradeBook::setCourseName( string name )
18   {
19      if ( name.size() <= 25 ) // if name has 25 or fewer characters
20         courseName = name; // store the course name in the object
21      else // if name is longer than 25 characters
22      { // set courseName to first 25 characters of parameter name
23         courseName = name.substr( 0, 25 ); // select first 25 characters
24         cerr << "Name "" << name << "" exceeds maximum length (25). "
25            << "Limiting courseName to first 25 characters. " << endl;
26      } // end if...else
27   } // end function setCourseName
28
29   // function to retrieve the course name
30   string GradeBook::getCourseName() const
31   {
32      return courseName;
33   } // end function getCourseName
34
35   // display a welcome message to the GradeBook user
36   void GradeBook::displayMessage() const
37   {
38      cout << "Welcome to the grade book for " << getCourseName() << "! "
39         << endl;
40   } // end function displayMessage
41
42   // determine class average based on 10 grades entered by user
43   void GradeBook::determineClassAverage() const
44   {
45      // initialization phase
46      int total = 0; // sum of grades entered by user
47      unsigned int gradeCounter = 0; // number of grades entered
48
49      // processing phase
50      // prompt for input and read grade from user  
51      cout << "Enter grade or -1 to quit: ";        
52      int grade = 0; // grade value                 
53      cin >> grade; // input grade or sentinel value
54
55      // loop until sentinel value read from user   
56      while ( grade != -1 ) // while grade is not -1
57      {
58         total = total + grade; // add grade to total
59         gradeCounter = gradeCounter + 1; // increment counter
60
61         // prompt for input and read next grade from user
62         cout << "Enter grade or -1 to quit: ";           
63         cin >> grade; // input grade or sentinel value   
64      } // end while
65
66      // termination phase
67      if ( gradeCounter != 0 ) // if user entered at least one grade...
68      {
69         // calculate average of all grades entered
70         double average = static_cast< double >( total ) / gradeCounter;
71
72         // display total and average (with two digits of precision)
73         cout << " Total of all " << gradeCounter << " grades entered is "
74            << total << endl;
75         cout << setprecision( 2 ) << fixed;
76         cout << "Class average is " << average << endl;
77      } // end if
78      else // no grades were entered, so output appropriate message
79         cout << "No grades were entered" << endl;
80   } // end function determineClassAverage


Fig. 4.10. Class average problem using sentinel-controlled repetition: GradeBook source code file.


 1   // Fig. 4.11: fig04_11.cpp
 2   // Create GradeBook object and invoke its determineClassAverage function.
 3   #include "GradeBook.h" // include definition of class GradeBook
 4
 5   int main()
 6   {
 7      // create GradeBook object myGradeBook and
 8      // pass course name to constructor
 9      GradeBook myGradeBook( "CS101 C++ Programming" );
10
11      myGradeBook.displayMessage(); // display welcome message
12      myGradeBook.determineClassAverage(); // find average of 10 grades
13   } // end main


Welcome to the grade book for
CS101 C++ Programming

Enter grade or -1 to quit: 97
Enter grade or -1 to quit: 88
Enter grade or -1 to quit: 72
Enter grade or -1 to quit: -1

Total of all 3 grades entered is 257
Class average is 85.67


Fig. 4.11. Class average problem using sentinel-controlled repetition: Creating a GradeBook object and invoking its determineClassAverage member function.

This example stacks control statements on top of one another—the while statement (lines 56–64 of Fig. 4.10) is immediately followed by an if...else statement (lines 67–79) in sequence. Much of the code in this program is identical to the code in Fig. 4.7, so we concentrate on the new features and issues.

Lines 46–47 initialize variables total and gradeCounter to 0, because no grades have been entered yet. Remember that this program uses sentinel-controlled repetition. To keep an accurate record of the number of grades entered, the program increments variable gradeCounter only when the user enters a grade value that is not the sentinel value and the program completes the processing of the grade. We declared and initialized variables grade (line 52) and average (line 70) where they are used. Notice that line 70 declares the variable average as type double. Recall that we used an int variable in the preceding example to store the class average. Using type double in the current example allows us to store the class average calculation’s result as a floating-point number. Finally, notice that both input statements (lines 53 and 63) are preceded by an output statement that prompts the user for input.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.246.47