Aug 24, 2017 at 1:00am UTC
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;
Aug 24, 2017 at 7:59am UTC
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 Aug 24, 2017 at 8:02am UTC
Aug 24, 2017 at 2:04pm UTC
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;
}
Aug 25, 2017 at 12:10am UTC
OMG that worked thank you soo much !!