Having trouble with functions

Hello, so this code is suppossed to calculate a student’s end of semester grade based on the following grade information: Midterm Exam Grade (20%), Final Exam Grade (20%), Lab Grade (50%) – Assume 4 Lab Assignments, and Quiz Grade (10%) – Assume 3 quizzes and drop the lowest quiz grade. So when I run it I come across 2 problems. A) inputAndAvgLabs always asks for 5 lab grades instead of 4, and B) the answer no matter what I input always comes out to 0. I don't know where I'm going wrong, any help/direction is much appreciated! Thanks!

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
#include <iostream>
#include <string>
using namespace std;

double inputExam(string examType);
double inputAndAvgLabs(); 
double inputAndAvgQuizzes();

int main()
{
	double final = 0.0;
    double midterm = 0.0;
    double quizzes = 0.0;
    double labs = 0.0;

    inputExam("Midterm");     
    midterm = midtermScore;        

	inputExam("Final");         
	final = finalScore;            

	inputAndAvgQuizzes();       
	quizzes = quizresult;         

	inputAndAvgLabs();         
	labs = labsresult;              

    semesterGrade = midterm*.20 + final*.20 + quizzes*.10 + labs*.50;
	cout << "Your End of Semester Grade is " << semesterGrade << endl;  

	system("pause");
	return 0; 
}

double inputExam (string examType)
{
	double midtermScore = 0.0;
	double finalScore = 0.0;

	if (examType == "Midterm")
	{
		cout << "Enter Midterm exam grade: ";
		cin >> midtermScore;
	}
	else if (examType == "Final")
	{
		cout << "Enter Final exam grade: ";
		cin >> finalScore;
	}
	
	return midtermScore;
	return finalScore;
}

double inputAndAvgQuizzes ()
{
	double lowest = 0.0;
	double quiz = 0.0;
	double total = 0.0;
	double quizresult = 0.0;
	
	cout << "Enter three quiz grades: ";
	cin >> quiz;
	quiz = lowest;
	cout << " ";

	for (int counter = 0; counter < 2; counter = counter + 1)
	{
		cin >> quiz;

		if (quiz < lowest)
		{	
			lowest = quiz;
		}

		total = total + quiz;
		total = total - lowest;
	}

	quizresult = total / 2;
	
	return quizresult;
}

double inputAndAvgLabs()
{
	double lab = 0.0;
	double total = 0.0;
	double labsresult = 0.0;
	
	cout << "Enter four lab grades: ";

	for (int counter = 0; counter < 3; counter = counter + 1)
	{
		cin >> lab;
		cout << " ";
		total = total + lab;
	}

	labsresult = total / 4;
	
	return labsresult;
}
Well A) I ran the loop in inputAndAvgLabs and it only asked me for 3 so I don't know how it's asking you for five but try this for your for loop
1
2
3
4
for(int counter = 0; counter <= 3; counter++)
{
     //code here
}


that ran 4 for me

then I copied your whole code, I couldn't even get it to run....it kept saying that all the variables you use in the functions are undeclared don't know why
That's odd, for me its always asking for 5, even if i replace it with...

 
for(int counter = 0; counter < 2; counter++)


so you can't get the whole code to work? Did I do something extremely wrong? I can get it to run, but it always comes out with 0 as the answer...
I think I spot 3 errors, the first one would be:
1
2
3
4
5
6
7
8
9
10
11
inputExam("Midterm");     
    midterm = midtermScore;        

	inputExam("Final");         
	final = finalScore;            

	inputAndAvgQuizzes();       
	quizzes = quizresult;         

	inputAndAvgLabs();         
	labs = labsresult; 


I think that should be:

1
2
3
4
5
6
7
8
        midterm = inputExam("Midterm");         

	final = inputExam("Final");                 

	quizzes = inputAndAvgQuizzes();       

	labs = inputAndAvgLabs();         
 


then, I think the return statements in the inputExam function should be inside the if block, since right now it's never going to get to return finalScore;

Finally, I think that the problem you had with inputAndAvgLabs was that it was only asking for 3 grades, at least it did for me. The code below works for me, the error was inside the for loop statement, it should either be counter < 4 or counter <= 3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

double inputAndAvgLabs(){
	double total = 0.0;
	double labsresult = 0.0;

	std::cout << "Enter four lab grades: ";

	for (int counter = 0; counter < 4; counter++){
        double lab = 0.0;
		std::cin >> lab;
		std::cout << std::endl;
		total += lab;
	}

	labsresult = total / 4;

	return labsresult;
}


Sorry if I made any mistakes, I'm pretty new to C++ too. Also, a lot of people agree it's bad practice to use any of the system commands, as it's exclusive to windows and there are other ways to get the same result.
Last edited on
Ok...I think I figured out what's going on...for some reason the changes I was making weren't taking when I went to debug it just used the old code so it's working now!! Now I just have a formatting question if that's not too much to ask... just to make it look prettier on the execution, how would I make inputs go on the same line for example:

Enter the quiz grades: 70 60 100

Rather than

Enter the quiz grades: 70

60
100
One way I know of would be taking the input as a string and then splitting it into 3 parts
hmmm ok also one last question! I'm just having an error here where I'm trying to pull the lowest quiz grade out so it won't be averaged, but it seems to be averaged in anyway, is there a better way to do this?

My 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
double inputAndAvgQuizzes ()
{
	double lowest = 0.0;
	double quiz = 0.0;
	double total = 0.0;
	double quizresult = 0.0;
	
	cout << "Enter three quiz grades: ";
	cin >> quiz;
	quiz = lowest;
	std::cout << std::endl;

	for (int counter = 0; counter < 2; counter = counter + 1)
	{
		cin >> quiz;

		if (quiz < lowest)
		{	
			lowest = quiz;
		}

		total = total + quiz;
		total = total - lowest;
	}

	quizresult = total / 2;
	
	return quizresult;
}
Line 10 should be lowest = quiz;.

You subtract the lowest score from the total every iteration of the for loop, so you need to move line 23 outside (and after) of the for loop.
Ok so I did that... I'm entering:

Midterm Exam Grade: 80
Final Exam Grade: 90
Quiz Grades: 70 60 100
Lab Grades: 90 50 75 45

I should get 75 as the Semester Grade but I'm now getting 71.5, before I was getting 74.5, I feel like I'm almost there, but am I just not seeing something?
Can you post your updated code?
Sure! Sorry, here's the whole thing: Quizzes start on lines 53

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
#include <iostream>
#include <string>
using namespace std;

double inputExam(string examType);
double inputAndAvgLabs(); 
double inputAndAvgQuizzes();

int main()
{
	double final = 0.0;
    double midterm = 0.0;
    double quizzes = 0.0;
    double labs = 0.0;
	double semesterGrade = 0.0;

    midterm = inputExam("Midterm");         

	final = inputExam("Final");                 

	quizzes = inputAndAvgQuizzes();       

	labs = inputAndAvgLabs();                

    semesterGrade = midterm*.20 + final*.20 + quizzes*.10 + labs*.50;
	cout << "Your End of Semester Grade is " << semesterGrade << endl;  

	system("pause");
	return 0; 
}

double inputExam (string examType)
{
	double midtermScore = 0.0;
	double finalScore = 0.0;

	if (examType == "Midterm")
	{
		cout << "Enter Midterm exam grade: ";
		cin >> midtermScore;
		
		return midtermScore;
	}
	else if (examType == "Final")
	{
		cout << "Enter Final exam grade: ";
		cin >> finalScore;
	
		return finalScore;
	}
}

double inputAndAvgQuizzes ()
{
	double lowest = 0.0;
	double quiz = 0.0;
	double total = 0.0;
	double quizresult = 0.0;
	
	cout << "Enter three quiz grades: ";
	cin >> quiz;
	lowest = quiz;
	std::cout << std::endl;

	for (int counter = 0; counter < 2; counter = counter + 1)
	{
		cin >> quiz;

		if (quiz < lowest)
		{	
			lowest = quiz;
		}

		total = total + quiz;
	}
	
	total = total - lowest;
	quizresult = total / 2;
	
	return quizresult;
}

double inputAndAvgLabs()
{
	double total = 0.0;
	double labsresult = 0.0;

	std::cout << "Enter four lab grades: ";

	for (int counter = 0; counter < 4; counter++)
	{
        double lab = 0.0;
		std::cin >> lab;
		std::cout << std::endl;
		total += lab;
	}

	labsresult = total / 4;

	return labsresult;
}
Last edited on
You input the first quiz grade before the loop and fail to add it to the total. So just change line 62 to
lowest = total = quiz; and it should work.
Last edited on
Ohhhhhhh, thank you so much, that worked, I forgot about that completely! Thanks for your patience and for your help, everyone!
Topic archived. No new replies allowed.