Need help with return functions

Hey guys. I have a programming class that I take once a week at my college. I had to miss last weeks class because of work, so I missed a ton of information. After reading through his notes online, I'm still lost. This is the last problem and I've been hung up on it for roughly an hour. This isn't the template code he gives you, this is what I've done after an hour. These are the instructions:

Modify the program above like this. We do not want to get scores, calculate and display their average in the main function. Instead, add a function getScoresFindAverage to perform these tasks. This function should return the average to main, so later it can be passed to toletterGrade to determine the letter grade.

This is the error I'm getting:

[Error] expected unqualified-id before '{' token

I'm assuming it's because I didn't do something with the getScoresFindAverage function in the beginning, but I'm really lost. Can anyone help?

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
// File: Lab21P2.cpp
// Created by Jason Goehring

#include <cstdlib>
#include <iostream>
using namespace std;

void toLetterGrade(double);

int main()
{
	double midterm = 0.0;
	double final = 0.0;
	double project = 0.0;
	double average = 0.0;
		
	cout << "Enter midterm score: ";
	cin >> midterm;
	cout << "Enter final score: ";
	cin >> final;	
	cout << "Enter project score: ";
	cin >> project;	
        cout << "Average Score: " << average << endl;

		
	system("pause"); 
	return 0;
}

void toLetterGrade(double average)
{ 
	char courseGrade = ' ';
	if (average >= 90)
	{
		courseGrade = 'A';
	}
	else if (average >= 80 && average < 90)
	{
		courseGrade = 'B';
	}
	else if (average >= 70 && average < 80)
	{
		courseGrade = 'C';
	}
	else if (average >= 60 && average < 70)
	{
		courseGrade = 'D';
	}
	else if (average < 60)
	{
		courseGrade = 'F';
	}
	cout << "Course grade: " << courseGrade << endl;
}    

double getScoresFindAverage(double);
{
	average = (midterm + final + project)/3;	
	return average;
}
Last edited on
Line 56: remove semicolon.
Now I get the following errors, all for line 58:

In function 'double getScoresFindAverage(double)':
[Error] 'average' was not declared in this scope
[Error] 'midterm' was not declared in this scope
[Error] 'final' was not declared in this scope
[Error] 'project' was not declared in this scope
You need to declare used variables. You probably want to pass midterm, final and project to function and declare local variable average, you will return later.
So I got it to compile, but it gives an average of 0. Any ideas?

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
// File: Lab21P2.cpp
// Created by Jason Goehring

#include <cstdlib>
#include <iostream>
using namespace std;

void toLetterGrade(double);
double getScoresFindAverage(double, double, double, double);

int main()
{
	double midterm = 0.0;
	double final = 0.0;
	double project = 0.0;
	double average = 0.0;
		
	cout << "Enter midterm score: ";
	cin >> midterm;
	cout << "Enter final score: ";
	cin >> final;	
	cout << "Enter project score: ";
	cin >> project;	
        cout << "Average Score: " << average << endl;

		
	system("pause"); 
	return 0;
}

void toLetterGrade(double average)
{ 
	char courseGrade = ' ';
	if (average >= 90)
	{
		courseGrade = 'A';
	}
	else if (average >= 80 && average < 90)
	{
		courseGrade = 'B';
	}
	else if (average >= 70 && average < 80)
	{
		courseGrade = 'C';
	}
	else if (average >= 60 && average < 70)
	{
		courseGrade = 'D';
	}
	else if (average < 60)
	{
		courseGrade = 'F';
	}
	cout << "Course grade: " << courseGrade << endl;
}    

double getScoresFindAverage(double average, double midterm, double final, double project)
{
	average = (midterm + final + project)/3;	
	return average;
}
1) you do not need to pass average to function as it is calculated there.
1
2
3
4
double getScoresFindAverage(double midterm, double final, double project)
{
    return (midterm + final + project)/3;
}

2) you do not change your average variable in main between declaration (line16) and output (line 24). Call your average function before output:
1
2
average = getScoresFindAverage(midterm, final, project);
cout << "Average Score: " << average << endl;
Got it to work, you are a god among men. I should've realized I wasn't calling average in main. I appreciate your help bro!
Do either of you guys mind looking over what I have here as well? I have run into the same problems discussed, and tried reviewing your suggestions, but can not get my program to run either.

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
// File : Lab 21 - Question 2
// Created by : Chase Wood 11/12/2014
// Scores on midterm, final, and project.

#include <cstdlib>
#include <iostream>
using namespace std;

	//Function Prototype
	//------
	
	double scoreaverage(double, double, double, double);
	void lettergrade(double);
	
	
int main()
{
	//Intro
	//-----
	cout << "DID YOU PASS OR FAIL?!" << endl;
	
	
	//Variables
	//------
	double midterm = 0.0;
	double final = 0.0;
	double project = 0.0;
	double average = 0.0;
	
	average = scoreaverage(midterm, final, project);

	//Input
	//-------
    cout << "Please enter midterm score: ";
	cin >> midterm;
	cout << "Please enter final score: ";
	cin >> final;	
	cout << "Please enter project score: ";
	cin >> project;	
	
	
	cout << "Average score: " << average << endl;

	
	
system("pause");
return 0;
}


void lettergrade(double average)
{ 
	char coursegrade = ' ';
	
	if (average >= 90)
	{
		coursegrade = 'A';
	}
	else if (average >= 80 && average < 90)
	{
		coursegrade = 'B';
	}
	else if (average >= 70 && average < 80)
	{
		coursegrade = 'C';
	}
	else if (average >= 60 && average < 70)
	{
		coursegrade = 'D';
	}
	else if (average < 60)
	{
		coursegrade = 'F';
	}
	cout << "Course grade: " << coursegrade << endl;
}   




double scoreaverage(double average, double midterm, double final, double project)
{
	
	return (midterm + final + project)/3;
	
	
}





I keep returning [Error] too few arguments to function
Last edited on
double scoreaverage(double average, double midterm, double final, double project)
Oh wow, I totally missed that..

I went through and changed the function prototype to..

1
2
	double scoreaverage(double, double, double);
	void lettergrade(double);


Back to only 3.

My program will compile and run, but still returns 0 for the average, and does not display the letter grade?
1
2
3
4
5
	double final = 0.0;
	double project = 0.0;
	double average = 0.0;
	
	average = scoreaverage(midterm, final, project);
Average of 0, 0 and 0 is 0. Everything works as written. You probably want to calculate average after getting input.
we must be in the same class.... wow.
Last edited on
Im having the same trouble, but instead its not displaying the course grade. Can someone look at my code and let me know what I am missing?

// File: Lab21P2.cpp
// Created by Melanie Gunn on 11/19/14

#include <cstdlib>
#include <iostream>
using namespace std;

void toLetterGrade(double);
double getScoresFindAverage(double, double, double);

int main()
{
double midterm = 0.0;
double final = 0.0;
double project = 0.0;
double average = 0.0;


cout << "Enter midterm score: ";
cin >> midterm;
cout << "Enter final score: ";
cin >> final;
cout << "Enter project score: ";
cin >> project;

average = getScoresFindAverage(midterm, final, project);
cout << "Average Score: " << average << endl;


system("pause");
return 0;
}

void toLetterGrade(double average)
{
char courseGrade = ' ';
if (average >= 90)
{
courseGrade = 'A';
}
else if (average >= 80 && average < 90)
{
courseGrade = 'B';
}
else if (average >= 70 && average < 80)
{
courseGrade = 'C';
}
else if (average >= 60 && average < 70)
{
courseGrade = 'D';
}
else if (average < 60)
{
courseGrade = 'F';
}
cout << "Course grade: " << courseGrade << endl;
}

double getScoresFindAverage(double midterm, double final, double project )
{
return (midterm + final + project)/3;
}
I don't see that you actually call the toLetterGrade() function that prints out the course grade.
can you give an example, im not sure where i should call the function that prints out the course grade other than how i did with "void toLetterGrade (double average)"
Function prototypes and definitions don't call the function to execute. You're calling a function (getScoresFindAverage) in main here.

average = getScoresFindAverage(midterm, final, project);

The letter grade function needs the average, so I'd call it after printing the average score.
thank you! You're great!
Topic archived. No new replies allowed.