You already have a 'global' (ok so it is local to the class, but all of the class methods can see it) data area defined. See this section under private in your class:
private:
double average;
string courseName [4];
char gradeLetter [4];
int gradePoint [4];
The only reason you aren't sharing the data is because you hide it by re-declaring the same variables inside your functions:
Here
void displayMessage ()
{
char gradeLetter [4] = {};
string courseName [4] = {""};
and here
double getAverage ()
{
int gradePoint [4] = {};
char gradeLetter [4] = {};
double average = 0.0;
You can just comment out (or delete) the declarations under each function and they will use the class data, sharing it between them.
I noticed why you aren't having to enter four grades for each class, it is the break inside the
for (y ... loop.
I might ask why you even have that loop anyway. Why not just do something like the following? (I have commented out your code where it is not needed or where I have modified it.)
void displayMessage ()
{
//char gradeLetter [4] = {};
//string courseName [4] = {""};
for ( int x = 0; x < 4; x++ )
{
cout<< "Please enter name of course:";
getline(cin, courseName [x]);
//for ( int y = 0; y < 4; y++ )
//{
cout<< "Enter grade recieved:";
//cin>> gradeLetter [y];
cin>> gradeLetter [x];
// break;
//}
cin.ignore (100, '\n');
}
system("cls");
for ( int x = 0; x < 4; x++ )
{
//for ( int y = 0; y < 4; y++ )
//{
//cout<< courseName[x]<< right<< setw (7)<< gradeLetter[y]<< '\n'<<'\n';
cout<< courseName[x]<< right<< setw (7)<< gradeLetter[x]<< '\n'<<'\n';
// break;
//}
}
}
When I was testing, I found another small bug. I gave the student 4 classes with grades of a, a, b, c and the GPA reported was 3, but it should have been 3.25
The average calculation was made using integer math because all of the parts in it were integer (even though the result is a double). I changed it to this:
average = (gradePoint[0] + gradePoint[1] + gradePoint[2] + gradePoint[3]) / 4.0;
The 4.0 on the end forces the division to be done in floating point math.