classes private data

ok so this program is supposed to end up calculating a final letter grade based on these 4 assessments. I have all the required functions but it isn't working out right and I am getting frustrated. I am new to this so i was wondering if anyone has any pointers to clean this up! thanks!


#include <iostream>
using namespace std;

class grading
{
public:
double setscores();
double getscores(double quiz1, double quiz2, double midterm, double finalexam):
void calcavg
void finalgrade

private:
double q1;
double q2;
double mid;
double final;
double average;
char lettergrade;
};

int main()
{
int n,i;
double q1,q2,mid,final,quiz1,quiz2,midterm,finalex;
grading student;

cout << "Enter the number of students:\n";
cin >> n;

for (i=0;i<n;i++)
{

student.setscores();
student.getscores(double quiz1, double quiz2, double midterm, double finalexam);
}

}

double grading::getscores()
{
double quiz1,quiz2,midterm,finalexam;
cout << "Enter the score for quiz #1:\n";
cin >> quiz1;
cout << "Enter the score for quiz #2:\n";
cin >> quiz2;
cout << "Enter the score for the midterm:\n";
cin >> midterm;
cout<< "Enter the score for the final exam:\n";
cin >> finalexam;
}

double grading::setscores(double quiz1, double quiz2, double midterm, double finalexam)
{
double q1,q2,mid,final;

q1=quiz1;
q2=quiz2;
mid=midterm;
final=finalexam;
}



void grading::calcavg()
{
double q1,q2,mid,final;
double total, total1, qq, mt, fe;
qq = (((q1+q2)/20)*.25);
mt = (mid/100)*.25;
fe = (final/100)*.50;
total1 = qq + mt + fe;
total = (total1 * 100);
}

void grading::finalgrade(double total)
{
cout << "The final grade is:" << total << " percent.";
if (total >= 90)
cout << "The student's grade is an A.\n";
else if ((total < 90) && (total >= 80))
cout << "The student's grade is a B.\n";
else if ((total < 80) && (total >= 70))
cout << "The student's grade is a C.\n";
else if ((total < 70) && (total >= 60))
cout << "The student's grade is a D.\n";
else
cout << "The student's grade is an F.\n";
}
change:
1
2
3
4
5
6
7
8
9
double grading::setscores(double quiz1, double quiz2, double midterm, double finalexam)
{
double q1,q2,mid,final;

q1=quiz1;
q2=quiz2;
mid=midterm;
final=finalexam;
}

to...
1
2
3
4
5
6
7
double grading::setscores(double quiz1, double quiz2, double midterm, double finalexam)
{
this.q1 = quiz1;
this.q2 = quiz2;
this.mid = midterm;
this.final = finalexam;
}


What you had was putting the values into local variables and not your class variables.
Have you even compiled what you posted? You have numerous compile errors.

Line 8: terminate the statement with; not :

Lines 9-10: These need ();

Line 34: Get rid of the types in the function call. finalexam is undefined.

Line 39: Implementation does not match the function declaration at line 8.

Line 41: These values will be lost when the function exits.

Line 52: Implementation does not match the function call.

Line 67: You are declaring total as a local variable. Your calculation at line 72 is not being saved. total goes out of scope when you exit the function.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
this is what I have now but its still not working and this one compiles

#include <iostream>
using namespace std;

class grading
{
public:
void getscores(double &quiz1, double &quiz2, double &midterm, double &finalexam);
void setscores(double quiz1, double quiz2, double midterm, double finalexam);
void calcavg(double q1,double q2,double mid,double final);
void finalgrade(double& total);

private:
double q1;
double q2;
double mid;
double final;
double average;
char lettergrade;
};

int main()
{
int n,i;
double q1,q2,mid,final,quiz1,quiz2,midterm,finalexam,total;
grading student;

cout << "Enter the number of students:\n";
cin >> n;

for (i=0;i<n;i++)
{

student.getscores(quiz1,quiz2,midterm,finalexam);
student.setscores(quiz1, quiz2, midterm, finalexam);
student.calcavg(q1,q2,mid,final);
student.finalgrade(total);
}

}

void grading::getscores(double &quiz1, double &quiz2, double &midterm, double &finalexam)
{

cout << "Enter the score for quiz #1:\n";
cin >> quiz1;
cout << "Enter the score for quiz #2:\n";
cin >> quiz2;
cout << "Enter the score for the midterm:\n";
cin >> midterm;
cout<< "Enter the score for the final exam:\n";
cin >> finalexam;
}

void grading::setscores(double quiz1, double quiz2, double midterm, double finalexam)
{
double q1,q2,mid,final;

q1=quiz1;
q2=quiz2;
mid=midterm;
final=finalexam;
}



void grading::calcavg(double q1,double q2,double mid,double final)
{
double total, total1, qq, mt, fe;

qq = (((q1+q2)/20)*.25);
mt = (mid/100)*.25;
fe = (final/100)*.50;
total1 = qq + mt + fe;
total = (total1 * 100);
}

void grading::finalgrade(double &total)
{
cout << "The final grade is:" << total << " percent.";
if (total >= 90)
cout << "The student's grade is an A.\n";
else if ((total < 90) && (total >= 80))
cout << "The student's grade is a B.\n";
else if ((total < 80) && (total >= 70))
cout << "The student's grade is a C.\n";
else if ((total < 70) && (total >= 60))
cout << "The student's grade is a D.\n";
else
cout << "The student's grade is an F.\n";
}
You were asked to use code tags. I will not provide further assistance until you edit your post and apply code tags.

Also, please don't post the same problem multiple times:
http://www.cplusplus.com/forum/beginner/130857/
Last edited on
your setScores should do what your getScores is doing. getScores shouldn't change anything it should only be used to get scores, get functions can usually be consts because of this while set functions purpose is usually to set the values of your private data members which you then utilize in your other functions. generally i don't like doing any I/O in a set function unless really necessary either, so in main you can set the values and run a loop that ends when the amount of students specified by user is reached. then pass the values to setGrades each time around the loop. you don't need to declare new variables in your set function, those will not hold values, just get rid of that and the set function will work as is to give values to the private members.
Last edited on
Topic archived. No new replies allowed.