Returning string function

If anyone could help me it would be greatly appreciated.

So, my code runs fine and I receive an average output correctly. However, although I do get a string value returned, I always get the last else if ( "Unfortunately you fail" when I run the program.. no matter what average is returned.

I have no idea, been in a slump.

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
include <iostream>             
#include <cmath>     
#include <cassert>
#include <conio.h>
#include <string>

using namespace std;

int AverageScore(int average ,int size, int grades, int sum);
string TestScore(int average);

int main() 
{
	int grades = 0;
	int	average = 0;
	int size = 0;
	int sum = 0;

		cout << "How many tests would you like to find the average to?" << endl;
		cin >> size;

		cout << "You're average is : " << AverageScore(size, grades, average, sum) << endl;
		
	    cout << TestScore(average);

		_getch();
		return 0;

}

int AverageScore( int size, int grades, int average, int sum)
{
	for (int i = 0; i != size; i++)
	{
		cout << "Enter a grade: ";
		cin >> grades;
		sum += grades;

	}
	system("pause");
	average = sum / size;
	return (average);	
	
}



string TestScore(int average)
{
	if (average >= 80)
		return "Congradulations, you got an A ";
	else if (average >= 70)
		return "You got a B";
	else if (average >= 60)
		return "You got a C";
	else if (average >= 50)
		return "You got a D";
	else if (average >= 0)
		return "Unfortunately you fail...";
	else
	{

		cerr << "Error : Your grade average was less than 0 or more than 100 ";
		return "";
	}

}



I can see that it keeps taking my local average value, but how would I make it use my value returned from my other function?

Thank you, Tyler
Last edited on
Quick reply: replace line 22 with:

1
2
average=AverageScore(size, grades, average, sum); 
cout << "You're average is : " << average<< endl;


However, I noticed a couple of issues with your program:

1. The order of parameters in AverageScore differs between declaration and implementation. That makes your code difficult to read, or it might give compiler errors.

2. From the list of parameters, grades, sum, and average are not used. What I mean to say that AverageScore(3,0,0,0) is the same as AverageScore(3,1,4,5). Sum and grades are not used in main, so you should not define them there

3. In general case, average should be a real number (float or double) not an int
Ahh, I feel ridiculous, that was the fix i needed , I should have picked that up.
Thank you.

However, I just need some clarification in your second pointer. I understand what you mean, but My program will only run with sum and grades defined in my main. Is it not needed for the statement you gave me ? ( average=AverageScore(size, grades, average, sum).

The function doesn't use the values of grades and average that is passed to the function so there is no point having them as function arguments. Instead you can declare them as local variables inside the function and let the function take only 2 arguments.

Are you ever going to pass in a value of sum that is not 0? If not, then you could get rid of that argument too.
Topic archived. No new replies allowed.