Need Help with code!! (Used code tags this time around.)

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: Need Help with code!! (Used code tags this time around.)

 
1
  #11
Jan 25th, 2009
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:
  1. private:
  2. double average;
  3. string courseName [4];
  4. char gradeLetter [4];
  5. 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
  1. void displayMessage ()
  2. {
  3. char gradeLetter [4] = {};
  4. string courseName [4] = {""};

and here
  1. double getAverage ()
  2. {
  3. int gradePoint [4] = {};
  4. char gradeLetter [4] = {};
  5. 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.)
  1. void displayMessage ()
  2. {
  3. //char gradeLetter [4] = {};
  4. //string courseName [4] = {""};
  5.  
  6. for ( int x = 0; x < 4; x++ )
  7. {
  8. cout<< "Please enter name of course:";
  9. getline(cin, courseName [x]);
  10.  
  11. //for ( int y = 0; y < 4; y++ )
  12. //{
  13. cout<< "Enter grade recieved:";
  14. //cin>> gradeLetter [y];
  15. cin>> gradeLetter [x];
  16. // break;
  17. //}
  18. cin.ignore (100, '\n');
  19. }
  20.  
  21. system("cls");
  22. for ( int x = 0; x < 4; x++ )
  23. {
  24. //for ( int y = 0; y < 4; y++ )
  25. //{
  26. //cout<< courseName[x]<< right<< setw (7)<< gradeLetter[y]<< '\n'<<'\n';
  27. cout<< courseName[x]<< right<< setw (7)<< gradeLetter[x]<< '\n'<<'\n';
  28. // break;
  29. //}
  30. }
  31. }

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:
  1. 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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 20
Reputation: Teethous is an unknown quantity at this point 
Solved Threads: 0
Teethous Teethous is offline Offline
Newbie Poster

Re: Need Help with code!! (Used code tags this time around.)

 
0
  #12
Jan 26th, 2009
Originally Posted by Teethous View Post
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;

}
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 20
Reputation: Teethous is an unknown quantity at this point 
Solved Threads: 0
Teethous Teethous is offline Offline
Newbie Poster

Re: Need Help with code!! (Used code tags this time around.)

 
0
  #13
Jan 26th, 2009
Originally Posted by Teethous View Post
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC