/*************************************************************
  File: grades.cpp
  Name: CDR B. A. Osborn
  Class 15
  Date: 9 February 2006
  Description: Reformulate Lab 4 Part 1 using a switch 
               statement vice nested if statements.
 ************************************************************/
#include <iostream>
using namespace std;
 
int main()
{
        double gpa;
        int total_points, total_hours, hours;
        char grade;
 
        total_hours = 0;
        total_points = 0;
 
        cout << "Input letter grade and hours (or Q to quit): ";
        cin >> grade;
 
        while ((grade != 'Q') && (grade !='q'))
        {
               cin >> hours;
 
                          switch (grade)
                          {
                                  case 'A': case 'a':
                                                     total_hours += hours;
                                                 total_points += (hours * 4);
                                                 break;
                                  case 'B': case 'b':
                                                 total_hours += hours;
                                                 total_points += (hours * 3);
                                                 break;
                                  case 'C': case 'c':
                                                 total_hours += hours;
                                                 total_points += (hours * 2);
                                                 break;
                                  case 'D': case 'd':
                                                 total_hours += hours;
                                                 total_points += (hours * 1);
                                                 break;
                                  case 'F': case 'f':
                                                 total_hours += hours;
                                                 total_points += (hours * 0);
                                                 break;
                                  default:
                                                 cout << "Invalid letter grade...try again!" << endl;
                          }
               cout << "Input letter grade and hours (or Q to quit): ";
               cin >> grade;
        }
 
               gpa = double(total_points)/double(total_hours);
 
               cout << "GPA = " << gpa << endl;
 
        return 0;
}
 
 

syntax highlighted by Code2HTML, v. 0.9.1