943,558 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 835
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Jan 25th, 2009
1

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

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:
c++ Syntax (Toggle Plain Text)
  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
c++ Syntax (Toggle Plain Text)
  1. void displayMessage ()
  2. {
  3. char gradeLetter [4] = {};
  4. string courseName [4] = {""};

and here
c++ Syntax (Toggle Plain Text)
  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.)
c++ Syntax (Toggle Plain Text)
  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:
c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Jan 26th, 2009
0

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

Click to Expand / Collapse  Quote originally posted by Teethous ...
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;

}
Reputation Points: 10
Solved Threads: 0
Light Poster
Teethous is offline Offline
32 posts
since Jan 2009
Jan 26th, 2009
0

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

Click to Expand / Collapse  Quote originally posted by Teethous ...
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.
Reputation Points: 10
Solved Threads: 0
Light Poster
Teethous is offline Offline
32 posts
since Jan 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Grr Linker problems?
Next Thread in C++ Forum Timeline: Need help reading a txt file into C++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC