class help

When I output the following code, it gives me default average and letter grade

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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161


//return an input integer within range ,start and end
int inputInteger(string prompt, int startRange, int endRange)
{

	int input;
	do
	{
		cout << prompt;
		if (!(cin >> input))
		{
			cout << "ERROR-3A: Invalid input. Must be an integer type.\n";
			cin.clear();
			cin.ignore(999, '\n');
		}
		else if (!(input >= min(startRange, endRange) && input <= max(startRange, endRange)))
			cout << "ERROR-3A: Invalid input. Must be from " << startRange << "..." << endRange << ".\n";
		else
			break;
	} while (true);
	return input;
}

class StudentRecord
{
private:
	int quiz1;
	int quiz2;
	int midtermExam;
	int finalExam;
	double averageScore;
	char letterGrade;
	void calculateAverageScore();
	void calculateLetterGrade();
public:
	StudentRecord();
	StudentRecord(int quiz1, int quiz2, int midtermExam, int finalExam);

	void setQuiz1(int quiz1);
	int getQuiz1()const;

	void setQuiz2(int quiz2);
	int getQuiz2()const;

	void setMidtermExam(int midtermExam);
	int getMidtermExam()const;

	void setFinalExam(int finalExam);
	int getFinalExam()const;

	double getAverageScore()const;
	char getLetterGrade()const;
};
StudentRecord::StudentRecord() :
	quiz1(0),
	quiz2(0),
	midtermExam(0),
	finalExam(0),
	averageScore(0.0),
	letterGrade('z'){};
StudentRecord::StudentRecord(int quiz1, int quiz2, int midtermExam, int finalExam)
{
	quiz1 >= 0 ? this->quiz1 = quiz1 : this->quiz1 = 0;
	quiz2 >= 0 ? this->quiz2 = quiz2 : this->quiz2 = 0;
	midtermExam >= 0 ? this->midtermExam = midtermExam : this->midtermExam = 0;
	finalExam >= 0 ? this->finalExam = finalExam : this->finalExam = 0;
}
void StudentRecord::setQuiz1(int quiz1)
{
	quiz1 >= 0 ? this->quiz1 = quiz1 : this->quiz1 = 0;
}
int StudentRecord::getQuiz1() const
{
	return quiz1;
}
void StudentRecord::setQuiz2(int quiz2)
{
	quiz2 >= 0 ? this->quiz2 = quiz2 : this->quiz2 = 0;
}
int StudentRecord::getQuiz2() const
{
	return quiz2;
}
void StudentRecord::setMidtermExam(int midtermExam)
{
	midtermExam >= 0 ? this->midtermExam = midtermExam : this->midtermExam = 0;
}
int StudentRecord::getMidtermExam() const
{
	return midtermExam;
}
void StudentRecord::setFinalExam(int finalExam)
{
	finalExam >= 0 ? this->finalExam = finalExam : this->finalExam = 0;
}
int StudentRecord::getFinalExam() const
{
	return finalExam;
}


double StudentRecord::getAverageScore() const
{
	return averageScore;
}
char StudentRecord::getLetterGrade() const
{
	return letterGrade;
}

void StudentRecord::calculateAverageScore()
{
	const int TOTAL_SCORE = 220;
	const double TOTAL_PERCENT = 100;

	double receivedScore =
		this->quiz1 +
		this->quiz2 +
		this->midtermExam +
		this->finalExam;

	this->averageScore = receivedScore / TOTAL_SCORE * TOTAL_PERCENT;
}
void StudentRecord::calculateLetterGrade()
{
	if (averageScore < 60) this->letterGrade = 'F';
	else if (averageScore < 70) this->letterGrade = 'D';
	else if (averageScore < 80) this->letterGrade = 'C';
	else if (averageScore < 90) this->letterGrade = 'B';
	else if (averageScore <= 100) this->letterGrade = 'A';
}

int main()
{
	StudentRecord studentGrades;// (quiz1, quiz2, midtermExam, finalExam);

	studentGrades.setQuiz1(inputInteger("Enter quiz1 score: ", 0, 10));
	studentGrades.setQuiz2(inputInteger("Enter quiz2 score: ", 0, 10));
	studentGrades.setMidtermExam(inputInteger("Enter midterm exam score: ", 0, 100));
	studentGrades.setFinalExam(inputInteger("Enter final exam score: ", 0, 100));

	cout << endl;

	gradeOutput(
		studentGrades.getQuiz1(),
		studentGrades.getQuiz2(),
		studentGrades.getMidtermExam(),
		studentGrades.getFinalExam(),
		studentGrades.getAverageScore(),
		studentGrades.getLetterGrade());


	cout << "Quiz 1: " << studentGrades.getQuiz1() << endl;
	cout << "Quiz 2: " << studentGrades.getQuiz2() << endl;
	cout << "Midterm Exam: " << studentGrades.getMidtermExam() << endl;
	cout << "Final Exam: " << studentGrades.getFinalExam() << endl;

	cout << "Average: " << studentGrades.getAverageScore() << endl;
	cout << "Letter Grade: " << studentGrades.getLetterGrade() << endl;
}

OUTPUT SAMPLE:

Quiz 1: 10
Quiz 2: 10
Midterm Exam: 100
Final Exam: 100
Average: 0.00
Letter Grade: z
Last edited on
Show us your main(), where you call (or don't call) calculateAverageScore() and calculateLetterGrade() before calling all the getters to output the data.
oh yes of course

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
void gradeOutput(int q1, int q2, int midterm, int final, double avg, char letter)
{
	cout << "Quiz 1: " << q1 << endl;
	cout << "Quiz 2: " << q2 << endl;
	cout << "Midterm Exam: " << midterm << endl;
	cout << "Final Exam: " << final << endl;

	cout << "Average: " << avg << endl;
	cout << "Letter Grade: " << letter << endl;
}
void Programming_Project2()
{
	StudentRecord studentGrades;// (quiz1, quiz2, midtermExam, finalExam);

	studentGrades.setQuiz1(inputInteger("Enter quiz1 score: ", 0, 10));
	studentGrades.setQuiz2(inputInteger("Enter quiz2 score: ", 0, 10));
	studentGrades.setMidtermExam(inputInteger("Enter midterm exam score: ", 0, 100));
	studentGrades.setFinalExam(inputInteger("Enter final exam score: ", 0, 100));

	cout << endl;

	gradeOutput(
		studentGrades.getQuiz1(),
		studentGrades.getQuiz2(),
		studentGrades.getMidtermExam(),
		studentGrades.getFinalExam(),
		studentGrades.getAverageScore(),
		studentGrades.getLetterGrade());


	//cout << "Quiz 1: " << studentGrades.getQuiz1() << endl;
	//cout << "Quiz 2: " << studentGrades.getQuiz2() << endl;
	//cout << "Midterm Exam: " << studentGrades.getMidtermExam() << endl;
	//cout << "Final Exam: " << studentGrades.getFinalExam() << endl;

	//cout << "Average: " << studentGrades.getAverageScore() << endl;
	//cout << "Letter Grade: " << studentGrades.getLetterGrade() << endl;
}
Like I said, where are calculateAverageScore() and calculateLetterGrade() called?
when I wrote this in the calculateAverageScore()
this->averageScore = receivedScore / TOTAL_SCORE * TOTAL_PERCENT;
it doesn't pass into the averageScore?? same with calculateLetterGrade()?

i get an error whenever i call those member functions in any other parts of the code...?
Last edited on
This doesn't work?
1
2
3
4
5
6
7
8
9
10
11
12
13
	studentGrades.setQuiz1(inputInteger("Enter quiz1 score: ", 0, 10));
	studentGrades.setQuiz2(inputInteger("Enter quiz2 score: ", 0, 10));
	studentGrades.setMidtermExam(inputInteger("Enter midterm exam score: ", 0, 100));
	studentGrades.setFinalExam(inputInteger("Enter final exam score: ", 0, 100));
	studentGrades.calculateAverageScore();
	studentGrades.calculateLetterGrade();
	gradeOutput(
		studentGrades.getQuiz1(),
		studentGrades.getQuiz2(),
		studentGrades.getMidtermExam(),
		studentGrades.getFinalExam(),
		studentGrades.getAverageScore(),
		studentGrades.getLetterGrade());
that code works except the getting the average and letter grade...

I have a header file where I have all the input validation.
User input can only be between 0-10 and 0-100

so how would one fix this?
I hate this life. Wrote everything and then it all got deleted -_-

I had to mess around with the code to get it to compile.

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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <iostream>
#include <string>

using namespace std;

class StudentRecord
{
private:
	int quiz1;
	int quiz2;
	int midtermExam;
	int finalExam;
	double averageScore;
	char letterGrade;
	void calculateAverageScore();
	void calculateLetterGrade();
public:
	StudentRecord();
	StudentRecord(int quiz1, int quiz2, int midtermExam, int finalExam);

	void setQuiz1(int quiz1);
	int getQuiz1()const;

	void setQuiz2(int quiz2);
	int getQuiz2()const;

	void setMidtermExam(int midtermExam);
	int getMidtermExam()const;

	void setFinalExam(int finalExam);
	int getFinalExam()const;

	double getAverageScore()const;
	char getLetterGrade()const;
};
StudentRecord::StudentRecord() :
	quiz1(0),
	quiz2(0),
	midtermExam(0),
	finalExam(0),
	averageScore(0.0),
	letterGrade('z') {};
StudentRecord::StudentRecord(int quiz1, int quiz2, int midtermExam, int finalExam)
{
	quiz1 >= 0 ? this->quiz1 = quiz1 : this->quiz1 = 0;
	quiz2 >= 0 ? this->quiz2 = quiz2 : this->quiz2 = 0;
	midtermExam >= 0 ? this->midtermExam = midtermExam : this->midtermExam = 0;
	finalExam >= 0 ? this->finalExam = finalExam : this->finalExam = 0;
}
void StudentRecord::setQuiz1(int quiz1)
{
	quiz1 >= 0 ? this->quiz1 = quiz1 : this->quiz1 = 0;
}
int StudentRecord::getQuiz1() const
{
	return quiz1;
}
void StudentRecord::setQuiz2(int quiz2)
{
	quiz2 >= 0 ? this->quiz2 = quiz2 : this->quiz2 = 0;
}
int StudentRecord::getQuiz2() const
{
	return quiz2;
}
void StudentRecord::setMidtermExam(int midtermExam)
{
	midtermExam >= 0 ? this->midtermExam = midtermExam : this->midtermExam = 0;
}
int StudentRecord::getMidtermExam() const
{
	return midtermExam;
}
void StudentRecord::setFinalExam(int finalExam)
{
	finalExam >= 0 ? this->finalExam = finalExam : this->finalExam = 0;
}
int StudentRecord::getFinalExam() const
{
	return finalExam;
}


double StudentRecord::getAverageScore() const
{
	return averageScore;
}
char StudentRecord::getLetterGrade() const
{
	return letterGrade;
}

void StudentRecord::calculateAverageScore()
{
	const int TOTAL_SCORE = 220;
	const double TOTAL_PERCENT = 100;

	double receivedScore =
		this->quiz1 +
		this->quiz2 +
		this->midtermExam +
		this->finalExam;

	this->averageScore = receivedScore / TOTAL_SCORE * TOTAL_PERCENT;
}
void StudentRecord::calculateLetterGrade()
{
	if (averageScore < 60) this->letterGrade = 'F';
	else if (averageScore < 70) this->letterGrade = 'D';
	else if (averageScore < 80) this->letterGrade = 'C';
	else if (averageScore < 90) this->letterGrade = 'B';
	else if (averageScore <= 100) this->letterGrade = 'A';
}

void gradeOutput(int q1, int q2, int midterm, int final, double avg, char letter)
{
	cout << "Quiz 1: " << q1 << endl;
	cout << "Quiz 2: " << q2 << endl;
	cout << "Midterm Exam: " << midterm << endl;
	cout << "Final Exam: " << final << endl;

	cout << "Average: " << avg << endl;
	cout << "Letter Grade: " << letter << endl;
}

int inputInteger(string prompt, int startRange, int endRange)
{
	double min;
	double max;

	if (startRange > endRange)
	{
		min = endRange;
		max = startRange;
	}

	else
	{
		min = startRange;
		max = endRange;
	}

	int input;
	do
	{
		cout << prompt;
		if (!(cin >> input))
		{
			cout << "ERROR-3A: Invalid input. Must be an integer type.\n";
			cin.clear();
			cin.ignore(999, '\n');
		}
		else if (!(input >= min && input <= max))
			cout << "ERROR-3A: Invalid input. Must be from " << startRange << "..." << endRange << ".\n";
		else
			break;
	} while (true);
	return input;
}

int main()
{
	StudentRecord studentGrades;// (quiz1, quiz2, midtermExam, finalExam);

	studentGrades.setQuiz1(inputInteger("Enter quiz1 score: ", 0, 10));
	studentGrades.setQuiz2(inputInteger("Enter quiz2 score: ", 0, 10));
	studentGrades.setMidtermExam(inputInteger("Enter midterm exam score: ", 0, 100));
	studentGrades.setFinalExam(inputInteger("Enter final exam score: ", 0, 100));


	cout << endl;

	gradeOutput(
		studentGrades.getQuiz1(),
		studentGrades.getQuiz2(),
		studentGrades.getMidtermExam(),
		studentGrades.getFinalExam(),
		studentGrades.getAverageScore(),
		studentGrades.getLetterGrade());
}
The issue with this code is that you never actually call these two functions:

1
2
void calculateAverageScore();
void calculateLetterGrade();


They're also private functions, so you can't call them at all.... Here's code that'll actually work as you intended:

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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <iostream>
#include <string>

using namespace std;

class StudentRecord
{
private:
	int quiz1;
	int quiz2;
	int midtermExam;
	int finalExam;
	double averageScore;
	char letterGrade;
public:
	void calculateAverageScore();
	void calculateLetterGrade();
	StudentRecord();
	StudentRecord(int quiz1, int quiz2, int midtermExam, int finalExam);

	void setQuiz1(int quiz1);
	int getQuiz1()const;

	void setQuiz2(int quiz2);
	int getQuiz2()const;

	void setMidtermExam(int midtermExam);
	int getMidtermExam()const;

	void setFinalExam(int finalExam);
	int getFinalExam()const;

	double getAverageScore()const;
	char getLetterGrade()const;
};
StudentRecord::StudentRecord() :
	quiz1(0),
	quiz2(0),
	midtermExam(0),
	finalExam(0),
	averageScore(0.0),
	letterGrade('z') {};
StudentRecord::StudentRecord(int quiz1, int quiz2, int midtermExam, int finalExam)
{
	quiz1 >= 0 ? this->quiz1 = quiz1 : this->quiz1 = 0;
	quiz2 >= 0 ? this->quiz2 = quiz2 : this->quiz2 = 0;
	midtermExam >= 0 ? this->midtermExam = midtermExam : this->midtermExam = 0;
	finalExam >= 0 ? this->finalExam = finalExam : this->finalExam = 0;
}
void StudentRecord::setQuiz1(int quiz1)
{
	quiz1 >= 0 ? this->quiz1 = quiz1 : this->quiz1 = 0;
}
int StudentRecord::getQuiz1() const
{
	return quiz1;
}
void StudentRecord::setQuiz2(int quiz2)
{
	quiz2 >= 0 ? this->quiz2 = quiz2 : this->quiz2 = 0;
}
int StudentRecord::getQuiz2() const
{
	return quiz2;
}
void StudentRecord::setMidtermExam(int midtermExam)
{
	midtermExam >= 0 ? this->midtermExam = midtermExam : this->midtermExam = 0;
}
int StudentRecord::getMidtermExam() const
{
	return midtermExam;
}
void StudentRecord::setFinalExam(int finalExam)
{
	finalExam >= 0 ? this->finalExam = finalExam : this->finalExam = 0;
}
int StudentRecord::getFinalExam() const
{
	return finalExam;
}


double StudentRecord::getAverageScore() const
{
	return averageScore;
}
char StudentRecord::getLetterGrade() const
{
	return letterGrade;
}

void StudentRecord::calculateAverageScore()
{
	const int TOTAL_SCORE = 220;
	const double TOTAL_PERCENT = 100;

	double receivedScore =
		this->quiz1 +
		this->quiz2 +
		this->midtermExam +
		this->finalExam;

	this->averageScore = receivedScore / TOTAL_SCORE * TOTAL_PERCENT;
}
void StudentRecord::calculateLetterGrade()
{
	if (averageScore < 60) this->letterGrade = 'F';
	else if (averageScore < 70) this->letterGrade = 'D';
	else if (averageScore < 80) this->letterGrade = 'C';
	else if (averageScore < 90) this->letterGrade = 'B';
	else if (averageScore <= 100) this->letterGrade = 'A';
}

void gradeOutput(int q1, int q2, int midterm, int final, double avg, char letter)
{
	cout << "Quiz 1: " << q1 << endl;
	cout << "Quiz 2: " << q2 << endl;
	cout << "Midterm Exam: " << midterm << endl;
	cout << "Final Exam: " << final << endl;

	cout << "Average: " << avg << endl;
	cout << "Letter Grade: " << letter << endl;
}

int inputInteger(string prompt, int startRange, int endRange)
{
	double min;
	double max;

	if (startRange > endRange)
	{
		min = endRange;
		max = startRange;
	}

	else
	{
		min = startRange;
		max = endRange;
	}

	int input;
	do
	{
		cout << prompt;
		if (!(cin >> input))
		{
			cout << "ERROR-3A: Invalid input. Must be an integer type.\n";
			cin.clear();
			cin.ignore(999, '\n');
		}
		else if (!(input >= min && input <= max))
			cout << "ERROR-3A: Invalid input. Must be from " << startRange << "..." << endRange << ".\n";
		else
			break;
	} while (true);
	return input;
}

int main()
{
	StudentRecord studentGrades;// (quiz1, quiz2, midtermExam, finalExam);

	studentGrades.setQuiz1(inputInteger("Enter quiz1 score: ", 0, 10));
	studentGrades.setQuiz2(inputInteger("Enter quiz2 score: ", 0, 10));
	studentGrades.setMidtermExam(inputInteger("Enter midterm exam score: ", 0, 100));
	studentGrades.setFinalExam(inputInteger("Enter final exam score: ", 0, 100));

	studentGrades.calculateAverageScore();
	studentGrades.calculateLetterGrade();

	cout << endl;

	gradeOutput(
		studentGrades.getQuiz1(),
		studentGrades.getQuiz2(),
		studentGrades.getMidtermExam(),
		studentGrades.getFinalExam(),
		studentGrades.getAverageScore(),
		studentGrades.getLetterGrade());
}



Notice that I've moved the two mentioned functions into the public space in the class and then called them from within int main()
That makes total sense, and I get that part...
The reason why I didn’t do it tht way is because they were assigned as provate in the UML i was given...? Now, that might have been a type I have to clarify with my professor.

There isn’t a way for it to be private??
There isn’t a way for it to be private??

Not if you wanna access it outside the class! You can keep it private and have another function that's inside the class and public call it.
gotcha. thanks master c':
Of course Young Grasshopper.
Topic archived. No new replies allowed.