GradeBook program

Hi all, I have a feeling I'll be one here often the next couple months. This is a homework question, but the due date has passed. I've been trying to figure out where I've gone wrong. I've spent over 24 hours on this code, and my instructor has not been helpful. I need to know what's wrong for future programs.

My Main:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include "GradeBook.h"
using namespace std;


int main() 
{ 	
	double labAvg = 0.0;
	double quizAvg = 0.0;
	double midtermGrade = 0.0;
	double finalGrade = 0.0;
	double ecGrade = 0.0;
	double finalSemesterGrade = 0.0;
	char courseGrade = ' ';
	
	GradeBook gradeBook1;
	
	double grade = 0.0;
	cout << fixed << setprecision(2);

	cout << "Enter the six lab grades: ";
	for (int i = 0; i < 6; i++)
	{
		cin >> grade;
			gradeBook1.addLabGrade(grade);
	}
	cout << "Enter the seven quiz grades: ";
	for (int i = 0; i < 7; i++)
	{
		cin >> grade;
			gradeBook1.addQuizGrade(grade);
	}
	gradeBook1.setMidtermGrade(grade);
	gradeBook1.setFinalGrade(grade);
	gradeBook1.setExtCreditGrade(grade);
	cout << "Average Lab Grade: " << gradeBook1.getAvgLabGrade() << endl;
	cout << "Average Quiz Grade: " << gradeBook1.getAvgQuizGrade() << endl;
	cout << "Midterm Exam Grade: " << gradeBook1.getMidtermGrade() << endl;
	cout << "Final Exam Grade: " << gradeBook1.getFinalGrade() << endl;
	cout << "Extra Credit Grade: " << gradeBook1.getExtCreditGrade() << endl;
	cout << "Final Semester Grade (FSG): " << gradeBook1.getFinalSemesterGrade() << endl;
	cout << "Final Letter Grade: " << gradeBook1.getLetterGrade() << endl;

	system("pause");
		return 0;
}


my header:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#ifndef GRADEBOOK_H
#define GRADEBOOK_H
#include <cstdlib>

class GradeBook
{
	
public:
	
   GradeBook();

   void addLabGrade(double grade);
   void addQuizGrade(double grade);
   void setMidtermGrade(double grade);
   void setFinalGrade(double grade);
   void setExtCreditGrade(double grade);
      
   double getAvgLabGrade();
   double getAvgQuizGrade();
   double getMidtermGrade();
   double getFinalGrade();
   double getExtCreditGrade();
   double getFinalSemesterGrade();
   char getLetterGrade();
   
private:
	
   double labGradeTotal;
   int labGradeCnt;
   double quizGradeTotal;
   int quizGradeCnt;
   double lowestQuiz;
   double labAvg;
   double quizAvg;
   double midtermGrade;
   double finalGrade;
   double ecGrade;
   double finalSemesterGrade;
   char finalLetterGrade;
};
#endif 


and my source code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include "GradeBook.h"
using namespace std;

GradeBook::GradeBook()
{
	double labGradeTotal = 0.0;
	int labGradeCnt = 0;
	double quizGradeTotal = 0.0;
	int quizGradeCnt = 0;
	double labAvg = 100.0;
	double quizAvg = 100.0;
	double lowestQuiz = 100.0;
	double midtermGrade = 0.0;
	double finalGrade = 0.0;
	double ecGrade = 0.0;
	double finalSemesterGrade = 0.0;
	char letterGrade = ' ';
}

void GradeBook::addLabGrade(double grade)
{
	labGradeTotal += grade;
	labGradeCnt = labGradeCnt +1;
}

void GradeBook::addQuizGrade(double grade)
{
	if (lowestQuiz > grade)
	{
		lowestQuiz = grade;
	}
	quizGradeTotal += grade;
	quizGradeCnt = quizGradeCnt + 1;
}

void GradeBook::setMidtermGrade(double grade)
{
	cout << "Enter midterm grade: ";
	cin >> midtermGrade;
}

void GradeBook::setFinalGrade(double grade)
{
	cout << "Enter final grade: ";
	cin >> finalGrade;	
}

void GradeBook::setExtCreditGrade(double grade)
{
	cout << "Extra credit grade: ";
	cin >> ecGrade;
}

double GradeBook::getAvgLabGrade()
{
	labAvg = labGradeTotal / labGradeCnt;
	return labAvg;
}

double GradeBook::getAvgQuizGrade()
{
	quizAvg = (quizGradeTotal - lowestQuiz) 
	/ (quizGradeCnt - 1);
	return quizAvg;	
}

double GradeBook::getMidtermGrade()
{
	return midtermGrade;
}

double GradeBook::getFinalGrade()
{
	return finalGrade;	
}

double GradeBook::getExtCreditGrade()
{
	return ecGrade;
}

double GradeBook::getFinalSemesterGrade()
{
	finalSemesterGrade = (labAvg * .5) + 
	(quizAvg * .1) + (midtermGrade * .2) + 
	(finalGrade * .2) + (ecGrade * .02);
	return finalSemesterGrade;
}

char GradeBook::getLetterGrade()
{
	if (finalSemesterGrade >= 90)
	{
		finalLetterGrade = 'A';
	}
	else if (finalSemesterGrade >= 80 && 
	finalSemesterGrade < 90)
	{
		finalLetterGrade = 'B';
	}
	else if (finalSemesterGrade >= 70 && 
	finalSemesterGrade < 80)
	{
		finalLetterGrade = 'C';
	}
	else if (finalSemesterGrade >= 60 && 
	finalSemesterGrade < 70)
	{
		finalLetterGrade = 'D';
	}
	else if (finalSemesterGrade < 60)
	{
		finalLetterGrade = 'F';
	}
	return finalLetterGrade;
};


When I plug numbers into the program, everything seems to work properly, with the exception that I get 0 for the lab and quiz averages, regardless of what I enter. I cannot figure out why this happens. I appreciate the help!

EDIT: In case it is not apparent, the quiz average function has the additional caveat of finding and dropping the lowest quiz grade from the calculation. I do not think this plays a role in my problem, but I wanted to explain the differences between the lab average function and quiz average function.
Last edited on
1
2
3
4
5
void foo(){
   int x = 1; //this is a local variable that resides only in this function
} //it would die when the function ends

//outside, nobody knows about it 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
int x = 54; //this is a global variable

void foo(){
   int x = 42; //this is a local variable, it has the same name that a global variable.
   //because of that, if we refer to `x' here it would refer to the local variable
   //the global variable has been hidden

   x *= 2; //so this would not affect the global variable
}

int main(){
   foo();
   std::cout << x << '\n'; //it would output 54
}


Based on that, ¿can you figure out what's the logic error in the next snip?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
GradeBook::GradeBook()
{
	double labGradeTotal = 0.0;
	int labGradeCnt = 0;
	double quizGradeTotal = 0.0;
	int quizGradeCnt = 0;
	double labAvg = 100.0;
	double quizAvg = 100.0;
	double lowestQuiz = 100.0;
	double midtermGrade = 0.0;
	double finalGrade = 0.0;
	double ecGrade = 0.0;
	double finalSemesterGrade = 0.0;
	char letterGrade = ' ';
}



Also ¿what's the purpose of these variables?
1
2
3
4
5
6
7
8
int main() 
{ 	
	double labAvg = 0.0;
	double quizAvg = 0.0;
	double midtermGrade = 0.0;
	double finalGrade = 0.0;
	double ecGrade = 0.0;
	double finalSemesterGrade = 0.0;


¿and why do you ask for a parameter that you never pretend to use?
1
2
3
4
5
void GradeBook::setFinalGrade(double grade)
{
	cout << "Enter final grade: ";
	cin >> finalGrade;	
}

First, thank you for the response!

I see what you are saying about global vs local variables. I actually removed all of those initializers from GradeBook.cpp and Lab2.cpp, and I received the same results, which leads me to believe that they were extraneous. Unfortunately, those results still do not include the lab or quiz averages. When I plug in 100 for all the grades, which should yield me a final result of 102 final semester grade (given the extra credit is worth an additional 2%) and a letter grade of A, I get:

Average Lab Grade: 0.00
Average Quiz Grade: -0.00
Midterm Grade: 100.00
Final Grade: 100.00
Extra Credit Grade: 100.00
Final Semester Grade: 42.00
Final Letter Grade: F

Given that it's calculating the grade with a 0 average for the labs and quizzes, the 42.00 final grade is correct. But I still do not understand why it does not calculate the averages correctly. Do you have anymore hints or advice to offer?

PS: I do actually use:

1
2
3
4
5
void GradeBook::setFinalGrade(double grade)
{
	cout << "Enter final grade: ";
	cin >> finalGrade;	
}


It's the function that asks the user for the final exam grade. I would rather have named it setFinalExamGrade to avoid confusion, but the names of the functions were not up to me in this exercise.
Last edited on
Initialize your variables
you pretend to use `labGradeTotal' as an accumulator and `labGradeCnt' as a counter, you they need to start at 0.

Note that you weren't doing that with
1
2
3
4
GradeBook::GradeBook()
{
	double labGradeTotal = 0.0;
	int labGradeCnt = 0;



¿what's the difference between the next calls?
1
2
3
4
gradeBook1.setFinalGrade(grade);
gradeBook1.setFinalGrade(42);
gradeBook1.setFinalGrade(0);
gradeBook1.setFinalGrade(-1);
you don't care about the parameter
Topic archived. No new replies allowed.