/* Programmer: Mark Fienup

Description: This program calculates a person's overall average GPA.

The user enters credit hours and letter grade per course. A sentinel value

of -999 for the credit hours is used to end the input.

*/

#include <stdio.h>

// function prototypes

double ReadGrade ();

char SkipBlanksAndTabs( );

char SkipBlanksAndTabsAndNewlines( );

int main() {

int credits;

int totalCredits;

double grade;

double totalGPAPoints;

char ch;

totalGPAPoints = 0;

totalCredits = 0;

printf( "Enter credit hour and the letter grades.\n" );

printf( "(Enter -999 for credit hour to end input.)\n" );

scanf("%d", &credits);

while ( credits != -999 ) {

totalCredits += credits;

grade = ReadGrade();

totalGPAPoints += grade * credits;

printf( "Enter credit hour and the letter grades.\n" );

printf( "(Enter -999 for credit hour to end input.)\n" );

scanf("%d", &credits);

} /* end while */

/* output summary of results */

printf( "\nOverall GPA is %.2f.\n", totalGPAPoints / totalCredits );

return 0; /* indicate program ended successfully */

} /* end function main */

/* This function reads from standard input to get a letter grade and returns

the corresponding GPA value, e.g., A is 4.0, A- is 3 2/3, etc. Some data

validation is perform and appropriate error messages being printed.

*/

double ReadGrade () {

double grade;

char ch;

/* skip white space to find first non-white space character */

ch = SkipBlanksAndTabsAndNewlines();

/* determine which grade was input */

switch ( ch ) { /* switch nested in while */

case 'A': /* grade was uppercase A */

case 'a': /* or lowercase a */

grade = 4.0;

break; /* necessary to exit switch */

case 'B': /* grade was uppercase B */

case 'b': /* or lowercase b */

grade = 3.0;

break; /* exit switch */

case 'C': /* grade was uppercase C */

case 'c': /* or lowercase c */

grade = 2.0;

break; /* exit switch */

case 'D': /* grade was uppercase D */

case 'd': /* or lowercase d */

grade = 1.0;

break; /* exit switch */

case 'F': /* grade was uppercase F */

case 'f': /* or lowercase f */

grade = 0.0;

break; /* exit switch */

default: /* catch all other characters */

printf( "Incorrect letter grade entered." );

printf( " Enter a new grade.\n" );

break; /* optional; will exit switch anyway */

} /* end switch */

/* skip white space to find a sign or newline or other */

ch = SkipBlanksAndTabs();

if (ch == '-') {

grade = grade - 1.0/3.0;

} else if (ch == '+') {

grade = grade + 1.0/3.0;

} else if (ch != '\n') {

printf("Error in grade modifier it can only be + or -.\n");

grade = 0.0;

} // end if

return grade;

} /* end GetGrades */

/* This function reads characters one at a time until it encounters something

other than a blank or a tab. The first non-blank or non-tab character is returned.

*/

char SkipBlanksAndTabs( ) {

char ch;

ch = getchar();

while ( ch == ' ' || ch == '\t' ) {

ch = getchar();

} // end while

return ch;

} /* end SkipBlanksAndTabs */

/* This function reads characters one at a time until it encounters something

other than a blank, a tab, or newline. The first non-blank, non-tab, or

non-newline character is returned.

*/

char SkipBlanksAndTabsAndNewlines( ) {

char ch;

ch = getchar();

while ( ch == ' ' || ch == '\t' || ch == '\n' ) {

ch = getchar();

} // end while

return ch;

} /* end SkipBlanksAndTabsAndNewlines */