Passing Functions issue in HW

Hello

I'm trying to build a program that takes scores from judges, drops the highest and lowest and averages the remaining three scores. Problem is, the program compiles but I keep getting zero as an average. What am I doing wrong???

Last edited on
Where in calcScore do you assign a value to avgOut?
uh I don't? So I have to put avgOut within the scope of the calcScore function? I think my main problem is I'm having problems understanding how reference variables work.
Last edited on
So I have to put avgOut within the scope of the calcScore function?

Parameters have function scope. The parameter avgerageOut is in scope in calcScore.


I think my main problem is I'm having problems understanding how reference variables work.

A reference is an alias (or another name) for a variable. Modifying avgerageOut in calcScore modifies avgOut in main.

(And I apologize for mistyping averageOut as avgOut in my first post. I'm sure that was a source of confusion.)

Last edited on
Thanks for the responses. This is what I have now and I'm still getting 0.00 as an average. (I didn't change anything in main.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main ()
{
    double avgOut=0;
    
    cout << fixed << showpoint << setprecision(2);
    
    cout << "-----------------------------------Star Search---------------------------------\n\n";
    cout << "Enter 5 scores for the contestant and I will drop the lowest and highest to find the average score."<<endl;
    
    getJudgesData (judge1);
    getJudgesData (judge2);
    getJudgesData (judge3);
    getJudgesData (judge4);
    getJudgesData (judge5);
    
    cout<<"Average Score: ";
    
    calcScore(judge1, judge2, judge3, judge4, judge5, avgOut);
    cout<< avgOut;




1
2
3
4
5
6
void calcScore (double num1, double num2, double num3, double num4, double num5, double &averageOut)
{
    double avgOut = 0;
    
    avgOut = ((num1 + num2 + num3 + num4 + num5) - findHighest(num1, num2, num3, num4, num5)-findLowest(num1, num2, num3, num4, num5))/3;
}
1
2
3
4
5
6
7
void calcScore (double num1, double num2, double num3, double num4, double num5, double &averageOut)
{
    double avgOut = 0;   // <- A local variable that stops existing when the function returns.
    
    // averageOut = ...
    avgOut = ((num1 + num2 + num3 + num4 + num5) - findHighest(num1, num2, num3, num4, num5)-findLowest(num1, num2, num3, num4, num5))/3;
}

I know I'm getting on your nerves lol but I appreciate your help. I'm still getting 0.00 as an average.

1
2
3
4
void calcScore (double num1, double num2, double num3, double num4, double num5, double &averageOut)
{
    averageOut = ((num1 + num2 + num3 + num4 + num5) - findHighest(num1, num2, num3, num4, num5)-findLowest(num1, num2, num3, num4, num5))/3;
}

In light of what we've just been over, does anything seem off about:
1
2
3
4
5
6
7
8
9
10
11
12
13
void getJudgesData (double &judgesScore)
{
    double score;
    
    cout << "Enter Judge's score: ";
    cin >> score;
    while (score<0 || score>10)
    {
        cout << "Score should be between 0 and 10."<<endl;
        cout << "Please enter a valid score: ";
        cin >> score;
    }
}
Last edited on
Does score stop existing when the function returns so the values stored are deleted? (Btw and I should have said this earlier but I must use void functions for getJudgesData and calcScore.)

ETA - I got it, yes! Thanks again. Your explanation really helped me understand the problem better.
Last edited on
Does score stop existing when the function returns so the values stored are deleted?

Yes, score is deleted when you exit getJudgesData().

cire's point and the reason he underlined the judgesScore argument is that you're passing it by reference so that it will be modified in the caller, but you never reference it.



Yes, score is deleted when you exit getJudgesData().

cire's point and the reason he underlined the judgesScore argument is that you're passing it by reference so that it will be modified in the caller, but you never reference it.


Thank you! These explanations have made it crystal clear now.
Topic archived. No new replies allowed.