write a program and an algorithm that determines a persons GPA, this will be based on taking only two classes. The GPA is found by using following formula:
GPA = Total Points / Total Number of Credits
The Total Points is found from taking the number of credits times the number value for corresponding letter grade and adding both classes together. The letter grades have following:
A = 4.0 B = 3.0 C = 2.0 D = 1.0 F = 0.0
We will not use and plus or minus grades, and we will assume user enters a capital letter only. The output will display the letter grade and number of credits for both classes. Once the GPA is determined its value will be displayed, and if the GPA is less than 2.0 a message will say “You are doing poorly” and if the GPA is 3.5 or above the message will display “Congratulations, doing good”. Below is sample output, test program with this and also B,3 A,4 and then with D,3 C,3.
Enter letter grade for first class --> B
Enter number of credit hours for first class --> 3
Enter letter grade for second class --> C
Enter number of credit hours for second class --> 4
B3
C4
Your GPA = 2.42857
while ( true )
{
cout << "Enter letter grade for first class --> ";
cin >> s[0].grade;
if ( s[0].grade >= 'A' && s[0].grade <= 'F' ) break;
cout << "Please enter A, B, C, D or F" << endl;
}
cout << "Enter number of credit hours for first class --> ";
cin >> s[0].credits;
Total_Points += number_value(s[0].grade) * (double) s[0].credits;
while ( true )
{
cout << "Enter letter grade for second class --> ";
cin >> s[1].grade;
if ( s[1].grade >= 'A' && s[1].grade <= 'F' ) break;
cout << "Please enter A, B, C, D or F" << endl;
}
cout << "Enter number of credit hours for second class --> ";
cin >> s[1].credits;
Total_Points += number_value(s[1].grade) * (double) s[1].credits;
double number_value ( char c )
{
switch ( c )
{
case 'A': return 4.0;
case 'B': return 3.0;
case 'C': return 2.0;
case 'D': return 1.0;
case 'F': return 0.0;
}
}
Well, your program works fine. I would change Total_Number_of_Credits type to int and get rid of casts. Or at least use C++ casts instead of C ones, it is easy to do something undesired with them.