Please send questions to
st10@humboldt.edu .
/*-----------------------------------------------------
Contract: main : void -> int
Purpose: quickie switch example --- asks how many
grades are to be entered, then prompts
the user for that many letter grades,
counting how many of each are entered.
Then prints a simple table of how many
of each has been entered, and then the
average of those grades (based on the classic
A = 4.0, B = 3.0, etc., scale).
Examples: If, when interactively prompted, the user
enters 12, and then: A b q a C b G B D a f b
then the program will print to the screen:
total number of grades entered: 12
number of A's: 3
number of B's: 4
number of C's: 1
number of D's: 1
number of F's: 1
number of bad entries: 2
Average of these letter grades: 2.7
by: Sharon M. Tuttle
last modified: 12-02-05
--------------------------------------------------------*/
#include <iostream>
#include "show_grade_freq.h"
#include "grade_avg.h"
using namespace std;
int main()
{
// declare variables
const int NUM_LETTER_GRADES = 6;
char curr_grade;
int num_each[NUM_LETTER_GRADES];
int num_grades;
// initialize each num_each element to 0
for (int i = 0; i < NUM_LETTER_GRADES; i++)
{
num_each[i] = 0;
}
// how many grades does the user want to enter?
cout << "How many letter grades are to be entered? ";
cin >> num_grades;
// ask user for that many letter grades
for (int i=0; i<num_grades; i++)
{
// ask user for a letter grade
cout << "Please enter a letter grade: ";
cin >> curr_grade;
// increment appropriate grade counter in num_each
switch(curr_grade)
{
case 'A':
case 'a':
num_each[0]++;
break;
case 'B':
case 'b':
num_each[1]++;
break;
case 'C':
case 'c':
num_each[2]++;
break;
case 'D':
case 'd':
num_each[3]++;
break;
case 'F':
case 'f':
num_each[4]++;
break;
default:
num_each[5]++;
cout << "hey, NOT a grade!!!";
cout << endl;
}
}
// print out a report showing how many of each grade
// there are
cout << endl
<< "total number of grades entered: " << num_grades
<< endl;
show_grade_freq(num_each, NUM_LETTER_GRADES);
// print out the average of all the grades, on a
// 4-point scale
cout << endl
<< "Average of these letter grades: "
<< grade_avg(num_each, NUM_LETTER_GRADES)
<< endl << endl;
return EXIT_SUCCESS;
}