classes/structures

ok So this program takes a students four scores and reports back their grade but it is not working and reports the grade F every time. I tried cout<< q1 and it gived bogus answers so i don't think anything was ever assigned to that variable then but I don't know how to fix it i am just a beginner thank you!




#include <iostream>
using namespace std;

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

private:
double q1;
double q2;
double mid;
double final;
};

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

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

for (i=0;i<n;i++)
{
student.getscores();
student.setscores(quiz1,quiz2,midterm,finalexam);
student.grade(q1,q2,mid,final);
}

}

void 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;
}

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

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



double grading::grade(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 = (total * 100);

cout << q1;
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";
}

closed account (j3Rz8vqX)
What happens in the below section, stays in the below section.
1
2
3
4
5
6
7
8
9
10
11
12
void grading::getscores()
{
double quiz1,quiz2,midterm,finalexam;//These data are only maintained here
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;
}


Why don't you design your getscores function like your setscores function?

Pass the values in by address =D
Last edited on
I realized that i was having that problem...the data was never being passed so i get junk answers, how do I fix that since I can't return anything?
closed account (j3Rz8vqX)
Function prototype:
void grading::getscores(double &quiz1,double &quiz2,double &midterm,double &finalexam);

Function header
1
2
3
4
void grading::getscores(double &quiz1,double &quiz2,double &midterm,double &finalexam)
{
    //stuff
}


Edit: Fixed a misplaced comma insertion.
Last edited on
Oh ok I was unaware that you could do that for these functions thank you!
Topic archived. No new replies allowed.