8 Replies - 479 Views - Last Post: 16 March 2012 - 08:11 PM Rate Topic: -----

#1 oAllElseFailo  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 23-September 11

Class/Object w/ if statements, confused!

Posted 13 March 2012 - 03:29 PM

So I basically have to write this student grades program and I have to use the class/object method here. Basically I have 2 functions in the class that have to determine the students grade (a/b/c) and their percentage. He wants the grade to be returned using a char (I don't even know if i'm doing it right) but i'm just running into problems left and right..

class StudentGrade
{
    public:
        double getFinalNumericGrade(int a, int b, int c)
        {
            qgrade = a;
            mgrade = b;
            fgrade = c;

            int first = a + b + c;
            double last = first/160;
            return last;
        }

        int getFinalLetterGrade()
        {
            char one, two, three, four, five;
            one = 'A';
            two = 'B';
            three = 'C';
            four = 'D';
            five = 'E';

            if(getFinalNumericGrade<=59)
            {
                return five;
            }
            else if(getFinalNumericGrade<=69)
            {
                return four;
            }
            else if(getFinalNumericGrade<=79)
            {
                return three;
            }
            else if(getFinalNumericGrade<=89)
            {
                return two;
            }
            else if(getFinalNumericGrade<=100)
            {
                return one;
            }

        }

    private:
        int qgrade, mgrade,fgrade;
};



I know I'm not calling the NumericGrade function correctly in the FinalGrade function and I heard there's a way to show if/else statements better but we haven't gone over it yet.. I'm just stuck completely and don't know what to do now. If someone can shed some light here on what I need to change or fix that'd be great :)

This post has been edited by oAllElseFailo: 13 March 2012 - 03:30 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Class/Object w/ if statements, confused!

#2 fastlane85  Icon User is offline

  • New D.I.C Head

Reputation: 9
  • View blog
  • Posts: 17
  • Joined: 13-March 10

Re: Class/Object w/ if statements, confused!

Posted 13 March 2012 - 03:55 PM

I'm not sure what the "getFinalNumericGrade" function is supposed to do, so I'm a bit confused, but I can see a couple of issues right away here.

First is one you already mentioned, and that is the calling of the function "getFinalNumericGrade" inside the function "getFinalLetterGrade". Any time you want to call a function you need to include parentheses and any necessary parameters, "getFinalLetterGrade" needs three. That being said your if/else-if statements need to call the function

...
 if(getFinalNumericGrade(a, b, c) <=59)
 {
     return five;
 }
...          



Again I'm not clear on your requirements or what "getFinalNumericGrade" is supposed to do, so I'm not really sure what data needs to be passed to it (as 'a', 'b' and 'c').

The next big thing I notice here is the function "getFinalLetterGrade", which declares a method returning an int
...
int getFinalLetterGrade()
...


inside the function you are attempting to return data type char.
...
one = 'A';
...
return one;
...


If you truly want to return a char instead of an int, change the method to:
...
char getFinalLetterGrade()
...



As for additional help, it would be necessary to know how this class is being used, or at least have a look at some code that is using this class to understand your requirements better.

HTH!
Was This Post Helpful? 1
  • +
  • -

#3 oAllElseFailo  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 23-September 11

Re: Class/Object w/ if statements, confused!

Posted 13 March 2012 - 04:26 PM

Hm.. I'll try to explain more what everything should be doing along with the entire code i have so far

class StudentGrade
{
    public:
        double getFinalNumericGrade(int a, int b, int c)
        {
            qgrade = a;
            mgrade = b;
            fgrade = c;

            int first = a + b + c;
            double last = first/160;
            return last;
        }

        char getFinalLetterGrade()
        {
            char one, two, three, four, five;
            one = 'A';
            two = 'B';
            three = 'C';
            four = 'D';
            five = 'E';
            if(getFinalNumericGrade(last)<=59)
            {
                return five;
            }
            else if(getFinalNumericGrade<=69)
            {
                return four;
            }
            else if(getFinalNumericGrade<=79)
            {
                return three;
            }
            else if(getFinalNumericGrade<=89)
            {
                return two;
            }
            else if(getFinalNumericGrade<=100)
            {
                return one;
            }

        }

    private:
        int qgrade, mgrade,fgrade;
};

int main()
{
    StudentGrades StudentScores;
    StudentScores.

    int q, m , f;

    cout << "Please enter the quiz score (0-10):";
    cin >> q >> endl;
    cout << "Please enter the midterm score (0-50):";
    cin >> m >> endl;
    cout << "Please enter the final score (0-100):";
    cin >> f >> endl;

    getFinalNumericGrade(q, m , f);
}



So obviously its not complete, don't have to bother really looking at the main function yet. But after the user inputs the three grades, NumericGrade is supposed to calculate the 3 numbers and divide it by 160 (max points) and return a double (percent grade). Then the FinalLetterGrade is supposed to check to see if the user got an A B C D or F but he said he wants that to be returned as a char. After all that then i have a print out that should be going into the main function.

So in regards to what you posted. How can i call the NumericGrade function without using 3 parameters? I just want to use the double last i had it do to calculate the overall numeric grade.
Was This Post Helpful? 0
  • +
  • -

#4 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 687
  • View blog
  • Posts: 2,376
  • Joined: 31-December 10

Re: Class/Object w/ if statements, confused!

Posted 13 March 2012 - 05:19 PM

You should add a constructor that takes the three int arguments: 'a', 'b', and 'c'. That constructor will save those values in: qgrade, mgrade, and fgrade. Then the getFinalNumericGrade function doesn't need to take any parameters.

Your other problem is with how you get the letter grade. You're basing the grading off of 0-100. You're dealing with 3 tests that total up to 160. So you need change your logic to grade them correctly.

This post has been edited by vividexstance: 13 March 2012 - 05:47 PM

Was This Post Helpful? 0
  • +
  • -

#5 oAllElseFailo  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 23-September 11

Re: Class/Object w/ if statements, confused!

Posted 13 March 2012 - 07:30 PM

I'll try that with the constructor.

But the letter grade is right. The three test total to 160 points, the 0-100 is the percent grade.
Was This Post Helpful? 0
  • +
  • -

#6 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 687
  • View blog
  • Posts: 2,376
  • Joined: 31-December 10

Re: Class/Object w/ if statements, confused!

Posted 14 March 2012 - 09:40 AM

If the three test totals added up to 100 then maybe it would be right, but they don't so your logic for getting the letter grade is off.

Let's do a simple example, let's say that a user entered 10, 50, and 100 for each of the tests. That would mean they got an 'A' for each test, which should average out to an 'A'. You take the total of those test scores: 10 + 50 + 100 = 160 and then divide it by 160 which equals 1.0. You then use that value in your if-else-statements to return a specific letter grade. If the grade == 1.0 (which it does) then that function is going to return 'E' not 'A'. Is that correct?

This post has been edited by vividexstance: 14 March 2012 - 09:41 AM

Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5929
  • View blog
  • Posts: 12,851
  • Joined: 16-October 07

Re: Class/Object w/ if statements, confused!

Posted 14 March 2012 - 11:51 AM

Your class is kind of, can I put the delicately, pointless. You just have a couple of functions with little relation to anything.

class StudentGrade {
public:
   void setQuizScore(int);
   void setMidtermScore(int);
   void setFinalScore(int);
   
   int getQuizScore() const;
   int getMidtermScore() const;
   int getFinalScore() const;
   
   int getFinalGrade() const;
   char getFinalLetterGrade() const;
private:
   int quizScore, midtermScore, finalScore;
};



There's a framework that actually looks like a class. It has meaning because it stores values and operates on those values.

You can use it in your program:
StudentGrade sg;
int n

cout << "Please enter the quiz score (0-10):";
cin >> n;
sg.setQuizScore(n);

cout << "Please enter the midterm score (0-50):";
cin >> n;
sg.setMidtermScore(n);
//...



The grade thing is pretty simple:
char StudentGrade::getFinalLetterGrade() const {
	int grade = getFinalGrade();
	if(grade<60) { return 'F'; }
	if(grade<70) { return 'D'; }
	//...



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#8 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Class/Object w/ if statements, confused!

Posted 14 March 2012 - 06:55 PM

I agree with baavgai that the StudentGrade class, at least as you have implemented it, is pointless. OOP is all about objects, yes, but objects that mean something -- objects have attributes (member variables) and objects have methods (member functions) to manipulate those attributes. A grade doesn't seem to make much sense as an object because what does a grade have? Basically just a value.

But at least the outline that baavgai suggested looks like a class. You can more or less regard it as a "stripped-down" Student class, i.e,., a student minus the "human" attributes such as name, id number, major, etc. Maybe you'll flesh it out as the semester progresses.

Meanwhile, baavgai gave you some good suggestions. Have you made any progress with it?
Was This Post Helpful? 0
  • +
  • -

#9 oAllElseFailo  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 23-September 11

Re: Class/Object w/ if statements, confused!

Posted 16 March 2012 - 08:11 PM

edit: nvm fixed it all! wewt! Thanks for everyones help!

This post has been edited by oAllElseFailo: 16 March 2012 - 09:15 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1