HELP--Calculations not working out

Hi! Im doing a gradebook program for class, with that has a header file and a cpp file. Once i run the program the calculations for final semester grade come up as a really long negative number. fsd has been declared already and initialized

double gradebook::getFinalSemesterGrade()//calculates final semester grade
{

fsd = (lab_avg * .50) + (quiz_avg * .10) + (midterm_exam_grade * .20) + (final_exam_grade * .20) +
(ec_grade * .02);
return fsd;
}
closed account (NyqLy60M)
I don't believe your problem is in the code that's shown...
Are all of the variables shown here doubles?
yes, should i post all of the files, the .cpp's and the header file?
closed account (NyqLy60M)
That would be for the best.

Please add code tags where applicable.
Thank You!

Lab1 part b.cpp

#include <iostream>
#include "gradebook.h"
#include <iomanip>


using namespace std;

int main()
{
gradebook gb, gb2;// creates two gradebook objects
double lab_grade;
double quiz_grade;
double lab_avg;
double quiz_avg;
double midterm_exam_grade;
double final_exam_grade;
double fsd;
double ec_grade;


cout << "Enter the Six Lab Grades: ";//asks user for six lab grades
cin >> lab_grade;
gb.addLabGrade(lab_grade);
gb2.addLabGrade(lab_grade);
cin >> lab_grade;
gb.addLabGrade(lab_grade);
gb2.addLabGrade(lab_grade);
cin >> lab_grade;
gb.addLabGrade(lab_grade);
gb2.addLabGrade(lab_grade);
cin >> lab_grade;
gb.addLabGrade(lab_grade);
cin >> lab_grade;
gb.addLabGrade(lab_grade);
cin >> lab_grade;
gb.addLabGrade(lab_grade);

lab_avg = gb.getAvgLabGrade();

lab_avg = gb2.getAvgLabGrade();


cout << "Enter the Seven Quiz Grades: "; // asks user for 7 quiz grades
cin >> quiz_grade;
gb.addQuizGrade(quiz_grade);
gb2.addQuizGrade(quiz_grade);
cin >> quiz_grade;
gb.addQuizGrade(quiz_grade);
gb2.addQuizGrade(quiz_grade);
cin >> quiz_grade;
gb.addQuizGrade(quiz_grade);
gb2.addQuizGrade(quiz_grade);
cin >> quiz_grade;
gb.addQuizGrade(quiz_grade);
gb2.addQuizGrade(quiz_grade);
cin >> quiz_grade;
gb.addQuizGrade(quiz_grade);
cin >> quiz_grade;
gb.addQuizGrade(quiz_grade);
cin >> quiz_grade;
gb.addQuizGrade(quiz_grade);

quiz_avg = gb.getAvgQuizGrade();

quiz_avg = gb2.getAvgQuizGrade();


cout << "Enter Your Midterm Exam Grade: "; //asks user to put in midterm exam grade
cin >> midterm_exam_grade;
gb.setMidtermExamGrade(midterm_exam_grade);
gb2.setMidtermExamGrade(midterm_exam_grade);





cout << "Enter Your Final Exam Grade: ";// asks user to put in final exam grade
cin >> final_exam_grade;
gb.setFinalExamGrade(final_exam_grade);
gb2.setFinalExamGrade(final_exam_grade);

cout << "Enter Your Extra Credit Grade: ";//asks user for extra credit grade
cin >> ec_grade;
gb.setExtraCreditGrade(ec_grade);
gb2.setExtraCreditGrade(ec_grade);



//Output for Gradebook 1
cout << fixed << setprecision(2);// sets to two decimal places
cout << endl << endl<<"STUDENT FINAL SEMESTER GRADE CALCULATION"<<endl;
cout << "Average Lab Grade: " << gb.getAvgLabGrade() << endl;
cout << "Average Quiz Grade: " << gb.getAvgQuizGrade() << endl;
cout << "Midterm Exam Grade: " << gb.getMidtermExamGrade() << endl;
cout << "Final Exam Grade: " << gb.getFinalExamGrade() << endl;
cout << "Extra Credit Grade: " << gb.getExtraCreditGrade() << endl;
cout << "Final Semester Grade (FSG): " << gb.getFinalSemesterGrade() << endl;
cout << "Final Letter Grade: " << gb.getFinalLetterGrade() << endl;

//Output for Gradebook 2
cout << fixed << setprecision(2);//sets to two decimal places
cout << endl << endl<<"STUDENT FINAL SEMESTER GRADE CALCULATION FOR SECOND OBJECT"<<endl;
cout << "Average Lab Grade: " << gb2.getAvgLabGrade() << endl;
cout << "Average Quiz Grade: " << gb2.getAvgQuizGrade() << endl;
cout << "Midterm Exam Grade: " << gb2.getMidtermExamGrade() << endl;
cout << "Final Exam Grade: " << gb2.getFinalExamGrade() << endl;
cout << "Extra Credit Grade: " << gb2.getExtraCreditGrade() << endl;
cout << "Final Semester Grade (FSG): " << gb2.getFinalSemesterGrade() << endl;
cout << "Final Letter Grade: " << gb2.getFinalLetterGrade() << endl;

system("pause");
return 0;
}
gradebook.h

#include <string>
using namespace std;

class gradebook
{
public:
gradebook(); //member functions
void addLabGrade(double grade);
void addQuizGrade(double grade);
void setMidtermExamGrade(double grade);
void setFinalExamGrade(double grade);
void setExtraCreditGrade(double grade);
double getMidtermExamGrade();
double getFinalExamGrade();
double getFinalSemesterGrade();
char getFinalLetterGrade();
double getAvgLabGrade();
double getAvgQuizGrade();
double getExtraCreditGrade();

private://data members
double lab_grade_total;
double quiz_grade_total;
int lab_grade_cnt;
int quiz_grade_cnt;
double lab_avg;
double quiz_avg;
double midterm_exam_grade;
double final_exam_grade;
double fsd;
double ec_grade;
char letter_grade;

};




gradebook.cpp

#include <iostream>
#include "gradebook.h"

gradebook::gradebook()
{
lab_grade_total = 0.0;
lab_grade_cnt = 0;
quiz_grade_total = 0.0;
quiz_grade_cnt = 0;
fsd = 0.0;
letter_grade = ' ';
}

void gradebook::addLabGrade(double grade)
{
lab_grade_total += grade;
lab_grade_cnt++;
}
void gradebook::addQuizGrade(double grade)
{
quiz_grade_total += grade;
quiz_grade_cnt++;
}
void gradebook::setMidtermExamGrade(double grade)
{
midterm_exam_grade = grade;
}
void gradebook::setFinalExamGrade(double grade)
{
final_exam_grade = grade;
}
void gradebook::setExtraCreditGrade(double grade)
{
ec_grade = grade;
}
double gradebook::getMidtermExamGrade()
{
return midterm_exam_grade;
}
double gradebook::getFinalExamGrade()
{
return final_exam_grade;
}
double gradebook::getFinalSemesterGrade()//calculates final semester grade
{

fsd = (lab_avg * .50) + (quiz_avg * .10) + (midterm_exam_grade * .20) + (final_exam_grade * .20) +
(ec_grade * .02);
return fsd;
}
char gradebook::getFinalLetterGrade()//pieces together final letter grade
{
if( fsd > 89) letter_grade = 'A';
else if( fsd > 79) letter_grade = 'B';
else if( fsd > 69) letter_grade = 'C';
else if( fsd > 65) letter_grade = 'D';
else letter_grade = 'F';
return letter_grade;
}
double gradebook::getAvgLabGrade()//calculate average here
{
return lab_grade_total / lab_grade_cnt;
}
double gradebook::getAvgQuizGrade()
{
return quiz_grade_total / quiz_grade_cnt;
}
double gradebook::getExtraCreditGrade()
{
return ec_grade;
}
closed account (NyqLy60M)
The gradebook() constructor should use a member initialization list.
http://www.cplusplus.com/forum/articles/17820/

As a precaution, you should add this to gradebook.h:
1
2
3
4
5
6
7
8
#ifndef GRADEBOOK_H
#define GRADEBOOK_H

// includes and etc.

class gradebook{ ... };

#endif 

Some information on include guards: http://stackoverflow.com/questions/1653958/why-are-ifndef-and-define-used-in-c-header-files

When the method "getFinalSemesterGrade()" is called,
lab_avg = -9.2559631349317831e+061,
and quiz_avg = lab_avg.

Let's see if you can figure out why.
Last edited on
I cant figure it out, whatever im doing wrong. I kept reading those articles and it still makes no sense to me. I am assuming my calculations with the lab avg and quiz avg is wrong, but those calculations were given by my teacher, so im not sure whats wrong.
closed account (NyqLy60M)
You're using the wrong lab_avg and quiz_avg.

In gradebook.h
 
double getFinalSemesterGrade(double, double);


In gradebook.cpp
1
2
3
4
5
6
7
double gradebook::getFinalSemesterGrade(double lab_avg, double quiz_avg)
{
    fsd = (lab_avg * .50) + (quiz_avg * .10) + (midterm_exam_grade * .20)
    + (final_exam_grade * .20) + (ec_grade * .02);

    return fsd;
}


Make sure EVERY variable gets initialized, in your main() source and elsewhere, and remove lab_avg and quiz_avg inside gradebook.h.
Last edited on
Topic archived. No new replies allowed.