(Grade Point Average) Modify the program of Fig. 4.9 to calculate the grade-point average. A grade of A is worth 4 points, B is worth 3 points, and so on.
What will be an ideal response?
```
// Create GradeBook object, input grades and display grade report.
#include
#include
using namespace std;
int main()
{
int aCount = 0; // count of A grades
int bCount = 0; // count of B grades
int cCount = 0; // count of C grades
int dCount = 0; // count of D grades
int fCount = 0; // count of F grades
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 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 '\n': // ignore newlines,
case '\t': // tabs,
case ' ': // and spaces in input
break; // exit switch
default: // catch all other characters
cout << "Incorrect letter grade entered."
<< " Enter a new grade.\n";
break; // optional; will exit switch anyway
} // end switch
} // end while
// display summary of results
cout << "\n\nNumber of students who received each letter grade:"
<< "\nA: " << aCount // display number of A grades
<< "\nB: " << bCount // display number of B grades
<< "\nC: " << cCount // display number of C grades
<< "\nD: " << dCount // display number of D grades
<< "\nF: " << fCount // display number of F grades
<< endl;
// calculate total grades
int gradeCount = aCount + bCount + cCount + dCount + fCount;
// 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 << "\nThe class average is: "
<< static_cast< double > ( gradeTotal ) / gradeCount
<< endl;
} // end if
} // end main
```
You might also like to view...
You can use a text box's Focus method to send the focus to the text box during run time.
Answer the following statement true (T) or false (F)
The ________ text wrap option has the text follow the shape of the object, filling any open spaces in the shape
Fill in the blank(s) with correct word
Explain the PAP authentication two-way handshake
What will be an ideal response?
A ________ is a device that combines the functionality of a personal digital assistant (PDA) and the mobility of a cellular phone
Fill in the blank(s) with the appropriate word(s).