(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 // parameterized stream manipulators
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
```

Computer Science & Information Technology

You might also like to view...

A smartphone has the capability to

A) Make a call to anywhere in the world without the Internet, do video, connect wireless. B) Make a call, download music from the Internet, but cannot run apps or track movement with GPS. C) Make a call, record calls received, play music from the Internet, but does not take pictures or video. D) Make a call, run apps, play music, track movement with GPS, connect wireless, or take pictures.

Computer Science & Information Technology

5Gb/s USB ports are commonly colored teal

Indicate whether the statement is true or false

Computer Science & Information Technology