Using functions to display a Grade

I understand the concept of the function being declared globally and locally and the calling of the function but I think I'm just missing some kind of syntax error that I'm not understanding. I get 2 errors, one about not having the { on line 24

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
//display total points earned and the grade
//created by jett

#include <iostream>

using std::endl;
using std::cin;
using std::cout;

int getScore(int, int, char);

int main ()
{
	int score = 0;
	int totalPoints = 0;
	char grade = 0;

	cout << "Enter the first score (negative number to stop):";
	cin >> getScore(grade,score,totalPoints);
return 0;
}

int getScore (int score, int totalPoints, char grade);
{
	while (score >= 0)
	{
		totalPoints += score;
		cout << "Next score (negative number to stop): ";
		cin >> score;
	if (totalPoints >= 360)
		grade = 'A';
	else if (totalPoints >= 320)
		grade = 'B';
	else if (totalPoints >= 280)
		grade = 'C';
	else if (totalPoints >= 240)
		grade = 'D';
	else grade = 'F';

	cout << endl;
	cout << "Total points: " << totalPoints << endl;
	cout << "Grade: " << grade << endl;
	}
return grade;
}

You declare the function on line 10:
int getScore(int, int, char);

Then you attempt to define the function on line 23:
int getScore (int score, int totalPoints, char grade);

but you only declared it again. Get rid of the semicolon at the end of the line, and the compiler will understand that what follows is the body of the function.
LINE 19--error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
Again, you cant use cin >> on that function.

You have to pass variables to it or use variables as reference to it.

Ok. I removed all the
cin >>
an now I'm getting an endless loop. I didn't know that you can't use
cin >>


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
//display total points earned and the grade
//created by jett

#include <iostream>

using std::endl;
using std::cin;
using std::cout;

int getScore(int, int, char);

int main ()
{
	int score = 0;
	int totalPoints = 0;
	char grade = 0;

	cout << "Enter the first score (negative number to stop):" << getScore(grade,score,totalPoints);
return 0;
}

int getScore (int score, int totalPoints, char grade)
{
	while (score >= 0)
	{
		totalPoints += score;
		cout << "Next score (negative number to stop): ";
	if (totalPoints >= 360)
		grade = 'A';
	else if (totalPoints >= 320)
		grade = 'B';
	else if (totalPoints >= 280)
		grade = 'C';
	else if (totalPoints >= 240)
		grade = 'D';
	else grade = 'F';

	cout << endl;
	cout << "Total points: " << totalPoints << endl;
	cout << "Grade: " << grade << endl;
	}
return score;
}
You can use cin >>. And you should.

You just can't use cin >> on a function. The value on the right of the >> operator must be a variable.

1
2
3
4
5
int var;
int function();

cin >> var;  // makes sense, gets a number from the user, puts it in 'var'
cin >> function();  // makes no sense!  where do you expect that number to go? 
Ok. I understand that. I don't understand how i'm getting errors telling me that 'totalPoints' and 'grade' are undeclared identifiers on line 14. Isn't it identified in the parameters?

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
//display total points earned and the grade
//created by jett

#include <iostream>

using std::endl;
using std::cin;
using std::cout;

int getScore(int score, int totalPoints, char grade);

int main ()
{
	int score = getScore(score, totalPoints, grade);

	cout << "Enter the first score (negative number to stop):" << endl;
	cin >> score;

return 0;
}

int getScore (int score, int totalPoints, char grade)
{
	while (score >= 0)
	{
		totalPoints += score;
		cout << "Next score (negative number to stop): ";

	if (totalPoints >= 360)
		grade = 'A';
	else if (totalPoints >= 320)
		grade = 'B';
	else if (totalPoints >= 280)
		grade = 'C';
	else if (totalPoints >= 240)
		grade = 'D';
	else grade = 'F';

	cout << endl;
	cout << "Total points: " << totalPoints << endl;
	cout << "Grade: " << grade << endl;
	}
return score;
}
I don't understand how i'm getting errors telling me that 'totalPoints' and 'grade' are undeclared identifiers on line 14. Isn't it identified in the parameters?


Nope, remember function parameters are diffrent from actual variables function paramerters are in essence placeholders for actual variables to go example:


int getScore (int score, int totalPoints, char grade)

is all well and true how ever when you want to use said function

int getScore(5, 4, A)

5 gets COPIED into score
4 gets COPIED into totalPoints
A gets COPIED into grade
Okay I understand now. I've got no syntax errors but I still have a problem with the code executing an endless loop.
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
//display total points earned and the grade
//created by jett

#include <iostream>

using std::endl;
using std::cin;
using std::cout;

int getScore(int totalPoints, char grade);

int main ()
{
	int totalPoints = 0;
	char grade = ' ';
	int score = getScore(totalPoints, grade);

	cout << "Enter the first score (negative number to stop): ";
	cin >> score;

return 0;
}

int getScore (int totalPoints, char grade)
{
	int score = 0;

	while (score >= 0)
	{
		totalPoints += score;
		cout << "Next score (negative number to stop): ";

	if (totalPoints >= 360)
		grade = 'A';
	else if (totalPoints >= 320)
		grade = 'B';
	else if (totalPoints >= 280)
		grade = 'C';
	else if (totalPoints >= 240)
		grade = 'D';
	else grade = 'F';

	cout << endl;
	cout << "Total points: " << totalPoints << endl;
	cout << "Grade: " << grade << endl;
	}
return score;
}
I don't see you reading in a score anywhere in the loop, so it stays zero and the loop never exits.
Ok. I understand what you're saying, but could you elaborate on where it's supposed to be read? I'm still new to this.
Your loop is waiting for 'score' to be less than zero, but you never change 'score' inside your loop.
Topic archived. No new replies allowed.