Hi. I'm David a newbie. I have just started Classes in C++ and I'm doing a GPA Calculator program. I have most of the program working but the average calculator seems to be giving me trouble. Please help. This is open to constructive criticism. All inputs will be gladly appreciated. Thank you in advance.

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

class GpaCalculator
    {
        public:


    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];
                     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';
                 break;
               }

            }
    }

double getAverage ()

 {
  char letter [4] = {'a', 'b', 'c', 'd'};
  char  gradeLetter [4] = {'a', 'b', 'c', 'd'};
  double average = 0.0;

     for ( int y = 0; y < 4; y++ )
      {

       if  (gradeLetter [y] == 'a')
           {
           gradeLetter [y] = 4;
           }


        else
        if (gradeLetter [y] == letter[1])
            {
            gradeLetter [y] = 3;
            }

          else
          if (gradeLetter [y] == letter[2])
              {
              gradeLetter [y] = 2;
              }

          else
          if (gradeLetter [y] == letter[3])
              {
              gradeLetter [y] = 1;
              }
    }

       average = ( gradeLetter [0] + gradeLetter [1] + gradeLetter [2] + gradeLetter [3] ) / 4;
       return average;

 }


        private:

        double average;
        string courseName [4];
        char  gradeLetter [4];
        char letter [4];
};



int main()
{

    GpaCalculator cal;

    cal.displayMessage ();


    cout<< "Your GPA is:"<< cal.getAverage ();
return 0;

}
double getAverage ()

 {
  char letter [4] = {'a', 'b', 'c', 'd'};
  char  gradeLetter [4] = {'a', 'b', 'c', 'd'};
  double average = 0.0;
private:

        double average;
        string courseName [4];
        char  gradeLetter [4];
        char letter [4];
};

You're going to get in trouble if you name your local variables the same as your class data members. I'm not sure which you intend to use where.

if  (gradeLetter [y] == 'a')
           {
           gradeLetter [y] = 4;
           }

This doesn't make sense, aside from the fact that you use the same name for the locals and the class variables. gradeLetter is defined as a character. Why are you assigning it a value of 4?

Your class has space for 4 class names and 4 grades.

But when you accept input from the user, you read a class name and then 4 grades for that class.

Then you read the next class name and 4 grades for the second class. (But you store the 4 grades from the second class where the first class did so you lost the 4 grades from the first class.)

Is your intent 4 classes with 1 grade each, or 4 classes with 4 grades each?

double getAverage ()

 {
  char letter [4] = {'a', 'b', 'c', 'd'};
  char  gradeLetter [4] = {'a', 'b', 'c', 'd'};
  double average = 0.0;
private:

        double average;
        string courseName [4];
        char  gradeLetter [4];
        char letter [4];
};

You're going to get in trouble if you name your local variables the same as your class data members. I'm not sure which you intend to use where.

if  (gradeLetter [y] == 'a')
           {
           gradeLetter [y] = 4;
           }

This doesn't make sense, aside from the fact that you use the same name for the locals and the class variables. gradeLetter is defined as a character. Why are you assigning it a value of 4?

The reason for assigning the value is for the calculation of the GPA. But I get what you are saying; I can't have an int vale of a char value. Right? This program should accept four "courses" along with the grade for each course respectively, then it should calculate the GPA for the cumulative grades with the "getAverage" function.

Your class has space for 4 class names and 4 grades.

But when you accept input from the user, you read a class name and then 4 grades for that class.

Then you read the next class name and 4 grades for the second class. (But you store the 4 grades from the second class where the first class did so you lost the 4 grades from the first class.)

Is your intent 4 classes with 1 grade each, or 4 classes with 4 grades each?

Hi. My intent is 4 classes with 1 grade for each class.This program should accept four "courses" along with the grade for each course respectively, then it should calculate the GPA for the cumulative grades with the "getAverage" function. It seems as if I need a link for the two functions but not finding how to do that. For example went the user enters the first "letterGrade" received, it would also updated the "gradeLetter" in the "getAverage" function. Hope this helps with you understanding what I'm getting at.

Thanks

Hi. My intent is 4 classes with 1 grade for each class.This program should accept four "courses" along with the grade for each course respectively, then it should calculate the GPA for the cumulative grades with the "getAverage" function. It seems as if I need a link for the two functions but not finding how to do that. For example went the user enters the first "letterGrade" received, it would also updated the "gradeLetter" in the "getAverage" function. Hope this helps with you understanding what I'm getting at.

Thanks

I would pick better names. I'd have letter or gradeLetter, but not both. You use gradeLetter to read in input, so I would delete letter altogether. If you want to calculate grade points, how about a variable called gradePoint?

Go through your if statements, but do this instead:

int gradePoint[4];

if (gradeLetter[y] == 'a')
    gradePoint[y] = 4;
else if (gradeLetter[i] == 'b')
    gradePoint[y] = 3;
// etc.

If you name your variables accurately, it'll help you notice when you try to assign an integer to a character or vice versa.

I tried you suggestion but it still isn't giving me the correct answer. If a person should get all A's in all 4 courses, they would have a 4.0 GPA. I'm getting 2 when I run my code with your suggestion.

When you run the code, are you still having to put the grades in 4 times or did you fix that?

Could you post your new code?

char  gradeLetter [4] = {'a', 'b', 'c', 'd'};

what do u think...

int gradeLetter [4] = {'a', 'b', 'c', 'd'};

When you run the code, are you still having to put the grades in 4 times or did you fix that?

Could you post your new code?

Is there a way for me to allow global access for the data entered into the "char gradeLetter" identifier of the "displayMessage ()" function? So that when I run the "getaverage ()" function these char values would already have been there, and it would just be a matter of comparing. I have placed my concern in RED. Oh I don't have a problem with the four grades. It's four courses and a letter grade for each.

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

class GpaCalculator
    {
        public:


    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];
                     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';
                 break;
               }

            }
    }

double getAverage ()

 {
  int gradePoint [4] = {};
  char  gradeLetter [4] = {};
  double average = 0.0;

     for ( int y = 0; y < 4; y++ )
      {

       if  (gradeLetter [y] == 'a')
           {
           gradePoint [y] = 4;
           }


        else
        if (gradeLetter [y] == 'b')
            {
            gradePoint [y] = 3;
            }

          else
          if (gradeLetter [y] == 'c')
              {
              gradePoint [y] = 2;
              }

          else
          if (gradeLetter [y] == 'd')
              {
              gradePoint [y] = 1;
              }
    }

       average = ( gradePoint [0] + gradePoint [1] + gradePoint [2] + gradePoint [3] ) / 4;
       return average;

 }


        private:

        double average;
        string courseName [4];
        char  gradeLetter [4];
        int gradePoint [4];
};



int main()
{

    GpaCalculator cal;

    cal.displayMessage ();


    cout<< "Your GPA is:"<< cal.getAverage ();
return 0;

}

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.

commented: Good advice. +11

Is there a way for me to allow global access for the data entered into the "char gradeLetter" identifier of the "displayMessage ()" function? So that when I run the "getaverage ()" function these char values would already have been there, and it would just be a matter of comparing. I have placed my concern in RED. Oh I don't have a problem with the four grades. It's four courses and a letter grade for each.

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

class GpaCalculator
    {
        public:


    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];
                     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';
                 break;
               }

            }
    }

double getAverage ()

 {
  int gradePoint [4] = {};
  char  gradeLetter [4] = {};
  double average = 0.0;

     for ( int y = 0; y < 4; y++ )
      {

       if  (gradeLetter [y] == 'a')
           {
           gradePoint [y] = 4;
           }


        else
        if (gradeLetter [y] == 'b')
            {
            gradePoint [y] = 3;
            }

          else
          if (gradeLetter [y] == 'c')
              {
              gradePoint [y] = 2;
              }

          else
          if (gradeLetter [y] == 'd')
              {
              gradePoint [y] = 1;
              }
    }

       average = ( gradePoint [0] + gradePoint [1] + gradePoint [2] + gradePoint [3] ) / 4;
       return average;

 }


        private:

        double average;
        string courseName [4];
        char  gradeLetter [4];
        int gradePoint [4];
};



int main()
{

    GpaCalculator cal;

    cal.displayMessage ();


    cout<< "Your GPA is:"<< cal.getAverage ();
return 0;

}

Hey! I must tell you thanks for all your help. You have shed so much light on my program. It's almost done it's just that I'm still getting a int value even though I have placed the 4.0 and also changed the data type for "gradePoint" to double. What is causing this problem?
View below:

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

class GpaCalculator
    {
        public:


    void displayMessage ()
    {

       for ( int x = 0; x < 4; x++ )

         {

             cout<< "Please enter name of course:";
             getline(cin, courseName [x]);



                     cout<< "Enter grade recieved:";
                     cin>> gradeLetter [x];
                    


                    cin.ignore (100, '\n');
         }

         system("cls");
         for ( int x = 0; x < 4; x++ )

            {
              cout<< courseName[x]<< right<< setw (7)<< gradeLetter[x]<< '\n'<<'\n';
            }
    }

double getAverage ()


{
    double average = 0.0;

     for ( int x = 0; x < 4; x++ )
      {

       if  (gradeLetter [x] == 'a')

           gradePoint [x] = 4.0;



        else
        if (gradeLetter [x] == 'b')
            {
            gradePoint [x] = 3.0;
            }

          else
          if (gradeLetter [x] == 'c')
              {
              gradePoint [x] = 2.0;
              }

          else
          if (gradeLetter [x] == 'd')
              {
              gradePoint [x] = 1.0;
              }

      }
       average = (gradePoint[0] + gradePoint[1] + gradePoint[2] + gradePoint[3])/4.0;
       return average;

 }


        private:


        string courseName [4];
        char  gradeLetter [4];
        double gradePoint [4];

};



int main()
{

    GpaCalculator cal;

    cal.displayMessage ();


    cout<< "Your GPA is:"<< cal.getAverage ();
return 0;

}

Hey! I must tell you thanks for all your help. You have shed so much light on my program. It's almost done it's just that I'm still getting a int value even though I have placed the 4.0 and also changed the data type for "gradePoint" to double. What is causing this problem?
View below:

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

class GpaCalculator
    {
        public:


    void displayMessage ()
    {

       for ( int x = 0; x < 4; x++ )

         {

             cout<< "Please enter name of course:";
             getline(cin, courseName [x]);



                     cout<< "Enter grade recieved:";
                     cin>> gradeLetter [x];
                    


                    cin.ignore (100, '\n');
         }

         system("cls");
         for ( int x = 0; x < 4; x++ )

            {
              cout<< courseName[x]<< right<< setw (7)<< gradeLetter[x]<< '\n'<<'\n';
            }
    }

double getAverage ()


{
    double average = 0.0;

     for ( int x = 0; x < 4; x++ )
      {

       if  (gradeLetter [x] == 'a')

           gradePoint [x] = 4.0;



        else
        if (gradeLetter [x] == 'b')
            {
            gradePoint [x] = 3.0;
            }

          else
          if (gradeLetter [x] == 'c')
              {
              gradePoint [x] = 2.0;
              }

          else
          if (gradeLetter [x] == 'd')
              {
              gradePoint [x] = 1.0;
              }

      }
       average = (gradePoint[0] + gradePoint[1] + gradePoint[2] + gradePoint[3])/4.0;
       return average;

 }


        private:


        string courseName [4];
        char  gradeLetter [4];
        double gradePoint [4];

};



int main()
{

    GpaCalculator cal;

    cal.displayMessage ();


    cout<< "Your GPA is:"<< cal.getAverage ();
return 0;

}

I have solved the problem thank you very much for your time and effort.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.