class problem!

hello i am trying to solve a problem in which a student inputs his grade for 2 quizzes each out of 10 pts and worth 25% of the over all grade, one midterm worth 100 points and 25% of the overall grade, and one final out of 100 points worth 50% of the grade. i want to calculate the grade of the student in the class according to what is enter. i need to use private members and 2 functions, one for the letter grade and another to figure out the percentage, and i need to ouput both. here is what i have so far. the problem i am facing is i cannot even print out the average (avg) of everything... i keep getting "Your average score is 0%" and i cannot figure for my LIFE why :( any help would be greatly appreciated!

thanks!



#include <iostream>

using namespace std;

class grades
{
public:
void totalaverage();
void finalgrade();

private:

int quiz1;
int quiz2;
int midterm;
int final;
int avg;

};

int main()
{
grades quizzes, exams;

cout << "entering grades \n";
quizzes.totalaverage();


return 0;
}

void grades::totalaverage()
{
cout << "enter quiz number 1" << endl;
cin >> quiz1;
cout << "enter quiz2" <<endl;
cin >> quiz2;
cout << "enter midterm" << endl;
cin >> midterm;
cout << "enter final" << endl;
cin >> final;

avg = quiz1/10+quiz2/10* 25 + midterm/100*25 + final/100*50;
cout << " Your average score is " << avg << "%"<< endl;
}

void grades::finalgrade()
{
if(avg >= 90)
{
cout << " you got an A" << endl;
}
else if(avg >= 80 && avg < 90)
{
cout << "you got a B" << endl;
}
else if(avg >= 70 && avg < 80)
{
cout << "you got a C" << endl;
}
else if(avg >= 60 && avg < 70)
{
cout << "you got a D" << endl;
}
else if(avg < 60)
{
cout << "you failed" << endl;
}
}
Last edited on
alright well now i got the percentage to work correcly howerever the finalgrade() function is not working... i have a feeling its just not using the same "avg" as for the previous function but i am not sure how to fix that... suggestions? here is my new code:


/*
* LAB_7_Ex_3.cpp
*
* Created on: Mar 15, 2011
* Author: Yohan
*/
#include <iostream>

using namespace std;

class grades
{
public:
void totalaverage();
void finalgrade();

private:

double quiz1;
double quiz2;
double midterm;
double final;
double avg;

};

int main()
{
grades quizzes, exams;


quizzes.totalaverage();

cout << "entering letter grade" << endl;
exams.finalgrade();

return 0;
}

void grades::totalaverage()
{
cout << "enter quiz number 1" << endl;
cin >> quiz1;
cout << "enter quiz2" <<endl;
cin >> quiz2;
cout << "enter midterm" << endl;
cin >> midterm;
cout << "enter final" << endl;
cin >> final;

avg = ((quiz1+quiz2)/20)* 25 + (midterm/100)*25 + (final/100)*50;
cout << " Your average score is " << avg << "%"<< endl;
}

void grades::finalgrade()
{
if(avg >= 90 && avg <= 100)
{
cout << " you got an A" << endl;
}
else if(avg >= 80 && avg < 90)
{
cout << "you got a B" << endl;
}
else if(avg >= 70 && avg < 80)
{
cout << "you got a C" << endl;
}
else if(avg >= 60 && avg < 70)
{
cout << "you got a D" << endl;
}
else if(avg < 60)
{
cout << "you failed" << endl;
}
}
I'm personally not a fan of using the std streams inside of member functions. This just gets to be a pain to unlearn if you plan on using multiple files, but at this point it's a style preference.

You are correct sir. Your 'main(...)' is declaring two different instances of the 'grades' object. One called 'quizzes' and the other called 'exams'. You seem to be loading your data into 'quizzes' but trying to display data from 'exams'. You fix that by picking one and deleting the other.
Great thank you!! it works like a charm! :)

you say are dont like using std streams... how would you it? i did like that because it seems the most logical but i'd love to know an alternative method!
you say are dont like using std streams

No, he said he doesn't like using them in member functions. Like, letting your class use cout. Which is technically ok when talking about using cout and stuff inside them, but I'd assume you would not object to overloading the stream operators for std streams geek?
oh yeah sorry that's what i meant... so would it be better to put the "cout" in the main instead? and is there a certain reason other than neatness why?
and is there a certain reason other than neatness why?
ico


There is a one class / one function - one responsibility rule. Of course they are situations in which you will break that rule because you would have to take unjustifiable expenses when you would abide it, but there is a reason why such a rule exists - it decreases the reusability of your class if it does more than one thing. Suppose you would want to output the quiz on a window instead of the terminal - you would have to completely rewrite the class as it is right now. Though in this case it's probably just some sort of homework thingy or so, in that case it doesn't really matter as you are probably not going to use that class ever again anyways.
Topic archived. No new replies allowed.