GPA Struct

So I can't get my program to run correctly:

#include<iostream>
#include<string>


using namespace std;

struct student
{
int credit[100];
char grade[100];
int numCourses;
void setGrades(int creditIn[], char gradeIn[], int numIn)
{
for (int i = 0; i < numIn; i++)
{
credit[i] = creditIn[i];
grade[i] = gradeIn[i];
numCourses = numIn;
}

};
void addGrade(int creditIn, char gradeIn)
{

for (int i = 5; i < 10; i++)
{
credit[10] = creditIn;
grade[10] = gradeIn;
numCourses++;
}

};
double getGPA(int gradeIn[])
{

double GPAVal = 0;
double TotGPAVal = 0;
double GPA = 0;

for (int i = 0; i < 7; i++)
if (gradeIn[i] == 'A')
{
GPAVal = 4.0;
TotGPAVal = TotGPAVal + GPAVal;
}
else if (gradeIn[i] == 'B')
{
GPAVal = 3.0;
TotGPAVal = TotGPAVal + GPAVal;
}
else if (gradeIn[i] == 'C')
{
GPAVal = 2.0;
TotGPAVal = TotGPAVal + GPAVal;
}
else if (gradeIn[i] == 'D')
{
GPAVal = 1.0;
TotGPAVal = TotGPAVal + GPAVal;
}
else if (gradeIn[i] == 'F')
{
GPAVal = 0.0;
TotGPAVal = TotGPAVal + GPAVal;
{
GPA = (TotGPAVal / 5);
}



};
return GPA;
};
};


int main()
{
cout << "Hello! Let's calculate your GPA... " << endl;
const int numStart = 5;
int startCredit[numStart] = { 3,4,3,2,4 };
char startGrade[numStart] = { 'A','A','B','C','B' };
student me;
student you;
me.setGrades(startCredit, startGrade, numStart);
you.setGrades(startCredit, startGrade, numStart);
me.addGrade(4, 'C');
me.addGrade(4, 'F');
you.addGrade(2, 'B');
you.addGrade(4, 'A');

cout << "I have a " << me.getGPA() << " GPA " << endl;
cout << " You have a " << you.getGPA()<< "GPA " << endl;
system("PAUSE");
return 0;



So you need to describe the problem better. What does it do that you don't like, or what does it not do that you think it should do? Does it compile? Are there any error messages?

This looks wrong:

1
2
3
4
5
6
7
8
else if (gradeIn[i] == 'F')
{
GPAVal = 0.0;
TotGPAVal = TotGPAVal + GPAVal;
    {
        GPA = (TotGPAVal / 5);
    }
};


You calculate the GPA when there is an F in the array. Doesn't make much sense.
Last edited on
so it is suppose to calculate the GPA from the values from me. addGrades and the you.addGrades

me.setGrades(startCredit, startGrade, numStart);
you.setGrades(startCredit, startGrade, numStart);
me.addGrade(4, 'C');
me.addGrade(4, 'F');
you.addGrade(2, 'B');
you.addGrade(4, 'A');

So I changed the GPA = (TotGPAVal / 5);
out side the program but now its giving me the same value.

#include<iostream>
#include<string>


using namespace std;

struct student
{
int credit[100];
char grade[100];
int numCourses;
void setGrades(int creditIn[], char gradeIn[], int numIn)
{
for (int i = 0; i < numIn; i++)
{
credit[i] = creditIn[i];
grade[i] = gradeIn[i];
numCourses = numIn;
}

};
void addGrade(int creditIn, char gradeIn)
{


credit[10] = creditIn;
grade[10] = gradeIn;
numCourses++;

};
double getGPA()
{


double GPAVal = 0;
double TotGPAVal = 0;
double GPA=0 ;

for (int i = 0; i < 7; i++)
if (grade[i] == 'A')
{
GPAVal = 4.0;
TotGPAVal = TotGPAVal + GPAVal;
}
else if (grade[i] == 'B')
{
GPAVal = 3.0;
TotGPAVal = TotGPAVal + GPAVal;
}
else if (grade[i] == 'C')
{
GPAVal = 2.0;
TotGPAVal = TotGPAVal + GPAVal;
}
else if (grade[i] == 'D')
{
GPAVal = 1.0;
TotGPAVal = TotGPAVal + GPAVal;
}
else if (grade[i] == 'F')
{
GPAVal = 0.0;
TotGPAVal = TotGPAVal + GPAVal;
{

}



};

GPA = (TotGPAVal / 5);
return GPA;
};
};


int main()
{
cout << "Hello! Let's calculate our GPA... " << endl;
const int numStart = 5;
int startCredit[numStart] = { 3,4,3,2,4 };
char startGrade[numStart] = { 'A','A','B','C','B' };
student me;
student you;
me.setGrades(startCredit, startGrade, numStart);
you.setGrades(startCredit, startGrade, numStart);
me.addGrade(4, 'C');
me.addGrade(4, 'F');
you.addGrade(2, 'B');
you.addGrade(4, 'A');

cout << "I have a " << me.getGPA() << " GPA " << endl;
cout << " You have a " << you.getGPA()<< " GPA " << endl;
system("PAUSE");
return 0;




}
Please use code tags. Edit your post, highlight the code and click the <> button to the right of the edit window.

addGrade() is wrong. It always modifies grade #10. The code should be:
1
2
3
        credit[nunmCourses] = creditIn;
        grade[numCourses] = gradeIn;
        numCourses++;


getGPA() is wrong. Why do you read exactly 7 grades? What if the student took 3 or 9 courses? Also, you compute TotGPAVal correctly, but then you aren't computing the GPA right. The GPA is TotGPAVal divided by the number of courses that the student took. So how many courses did they take?

When computing the GPA, should the courses be weighted by the number of credits?
OMG that worked thank you soo much !!
Topic archived. No new replies allowed.