I am new here and having trouble with my program at the moment. What I got so far is not doing the calculation. what i need the program to do is for the use to type in the course name and the grade, and calculate the grade to get the GPA. Is there an easy way to play with the array or which way would be better for me to approach this?
#include <iostream>
#include <string>
usingnamespace std;
struct GPA
{
string class1;
longlong total;
};
int main()
{
int n;
int answer;
int gpa;
char choice1;
char grade;
int A = 4;
int B = 3;
int C = 2;
int D = 1;
int F = 0;
int a = 0;
cout << "Would you like to enter a grade: Y or N ";
cin >> choice1;
if (choice1 == 'Y')
{
cout << "How many courses are we inputing: ";
cin >> n;
GPA * pointer = new GPA[n];
while (a < n)
{
for (int i = 0; i < n; i++)
{
cout << "Please enter the course name: ";
cin >> pointer[i].class1;
cout << "Please enter the grade in caps: ";
cin >> grade;
if (grade = 'A')
{
pointer[i].total = A;
}
if (grade = 'B')
{
pointer[i].total = B;
}
if (grade = 'C')
{
pointer[i].total = C;
}
if (grade = 'C')
{
pointer[i].total = C;
}
if (grade = 'D')
{
pointer[i].total = D;
}
if (grade = 'F')
{
pointer[i].total = F;
}
}
a++;
}
for (int k = 0; k < n; k++)
{
answer = pointer[k].total + 0;
}
gpa = answer / n;
cout << "Your GPA is: " << gpa << endl;
}
if (choice1 == 'N')
{
cout << "Program is closing... " << endl;
}
return 0;
}
Two, if you don't use a switch statement, use if-else. Forgetting the else is a bad programming habit.
Three, if you use if's and comparisons, don't forget the second equal sign. Your entire if-block assigns values to grade, instead of doing anything meaningful.
I tried a different way of doing the GPA by using the switch statement. A lot easier to use. what stopping me right now is adding up the inputs of the grades and getting the GPA. How would I get the inputs into an array or some type of storage and then calculate the GPA out?
the class is just there. still want it as an input and that's it.