C++ GPA programming

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
What have you done so far?
just have a try.
#include <iomanip>
#include <iostream>
using namespace std;

struct Student
{
char grade;
int credits;
};

double number_value ( char );

int main()
{
Student s[2];
double Total_Points = 0.0;
double Total_Number_of_Credits = 0.0;

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;

for ( unsigned i=0; i<2; i++ )
{
cout << s[i].grade << s[i].credits << endl;
Total_Number_of_Credits += (double) s[i].credits;
}

double GPA = Total_Points / Total_Number_of_Credits;
cout << "Your GPA = " << fixed << setprecision(5) << GPA << endl;
if ( GPA < 2.0 ) cout << "You are doing poorly" << endl;
if ( GPA >= 3.5 ) cout << "Congratulations, doing good" << endl;

return 0;
}

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.
ok thank you all!
Topic archived. No new replies allowed.