Need help with passing return values.

Hello! I'm taking a C++ class, and the midterm is coming up.I only have two more projects but so far they are whoopin my a**. I've tried for a couple hours to get the return values correctly passsed to the function before it and to no avail.

This code below is missing a major if statement but i have it copied and pasted elsewhere atm because its not needed right now.

my main issue is that i need to pass an average of hoursworked and payrate to the function GetGrades(), and then I need to have getgrades() pass it to main() without using any global variables and only using return values.

ive tried to look this up in a couple different places, becasue the class notes arent specific to this exact problem that im having, however, it is hard to tell what people are doing sometimes because they are using templates, or a completely different style, etc.

if someone could help me with returning the average value from FindAverage() back to GetGrades(), and then from GetGrades() back to main, i would be very happy with you :)

thank you for reading.

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
  /*    A program that prompts a user for their grades, finds the average and prints out a letter grade.
    The program calls a function called GetGrades that will read in grades from the keyboard, the number of grades should also be input by the user.
    GetGrades will find the sum of those grades and pass the sum and number of grades to another function called FindAverage.
    FindAverage will find the average of the grades and return this average to GetGrades.
    GetGrades will return this average to main.
    The main function will then determine the letter grade of that average based on a 10-point scale and print out this grade.
    90–100 A
    80–89 B
    70–79 C
    60–69 D
    0–59 F
    Sample Runs:
    Run1 – 30, 50, 90, 100.
    Run2 – 85, 89, 90, 95, 78, 99.
    Run3 – 85, 95.

    */

#include <iostream>

using namespace std;

float GetGrades(float);
float FindAverage(float, float);


int main()
{
    float returnVar;
    float average;
    GetGrades(average);

    //average=GetGrades(returnVar);
    cout << average << endl;
    return 0;
}

float GetGrades(float average)
{
    float gradeSum;
    float gradeNum;
    float studentGrade;

    cout << "How many grades do you have to input?" << endl;
    cin >> gradeNum;

    for(int num=1; num<=gradeNum; num++)
    {
        cout << "Enter a student grade" << endl;
        cin >> studentGrade;
        // add the positive number to sum everytime the loop cycles.
        gradeSum += studentGrade;

    }
    FindAverage(gradeSum, gradeNum);
    cout << FindAverage(gradeSum, gradeNum);
    average=FindAverage(gradeSum, gradeNum);
    return (average);
}

float FindAverage(float gradeSum, float gradeNum)
{
    return (gradeSum/gradeNum);
}
i wont be back until around 8PST because im off to work. any response would be great ,thanks!
Line 38: Why are you passing average into GetGrades? It's a return value, not an input value.

Line 40: gradeSum is uninitialized.

Line 52: You're going to be adding studentGrade to garbage.

Line 55: What's the point? It does nothing. i.e. the return value is discarded.


Thank you for the response! Would you be willing to show me an example of retreiving the return vallue? Im thoroughly confused at this point and reverse engineering will let me see the concept that I am missing. Or if you know a tutorial that is relevant, I will read it. Thanks again!

Edit: should I Initialize gradesum at 0? Ill try that when im off work
Last edited on
You're already retrieving the returned value at line 57.

Change lines 55-58 as follows:
55
56
57
  average=FindAverage(gradeSum, gradeNum);
  cout << average << endl;
  return average;


And yes, you should initialize gradesum to 0.
Last edited on
Thanks a bunch AbstractionAnon!
Topic archived. No new replies allowed.