Trouble with a function

I'm writing this program to take in the scores from contestants. There are 5 judges, so I'm accumulating the judges scores and sending them to the function to calculate the average. But, once I get to the second contestant, the average is an accumulation of both contestants. How do I get the function to only take each contestant at a time?

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
#include <iostream>
using namespace std;

void calcAvgScore (float);

int main() {

    string starName;
    float total = 0;
    int judgeGrade;
    
    do
    {
        cout<<"Enter the name of the star: ";
        cin>>starName;
    
            for (int i = 1; i <= 5; i++)
            {
                cout << "Enter judge "<<i<<" grade: ";
                int judgeGrade;
                cin >> judgeGrade;
                total = total + judgeGrade;
                //cout<<total;
                if (judgeGrade < 1 || judgeGrade > 10)
                {
                    cout<<"Please enter scores between 1 and 10 for judge "<<i<<": ";
                    cin>>judgeGrade;
                }
            }
            
        calcAvgScore(total);
    
    } while (starName != "Done");

    return 0;
}

//AVERAGE SCORE FUNCTION-------------------------------------
void calcAvgScore (float total)
{
    float starAverage = total / 5;
    
    cout<<"Average score: "<<starAverage<<endl;
}
Inside the do-while loop at the top you need to clear the total after you are done with it, I think.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 do
    {
        cout<<"Enter the name of the star: ";
        cin>>starName;
        total = 0;
            for (int i = 1; i <= 5; i++)
            {
                cout << "Enter judge "<<i<<" grade: ";
                int judgeGrade;
                cin >> judgeGrade;
                total = total + judgeGrade;
                //cout<<total;
                if (judgeGrade < 1 || judgeGrade > 10)
                {
                    cout<<"Please enter scores between 1 and 10 for judge "<<i<<": ";
                    cin>>judgeGrade;
                }
            }
            
        calcAvgScore(total);
    
    } while (starName != "Done"); // Total contains judges scores after each loop, accumulating the second contestants 
Last edited on
thank megatron 0, that's what I was missing.
thanks megatron 0, that's what I was missing.
You are very welcome, feel free to ask if you have any more problems.
Okay, so I have another issue. I haven't learned how to use arrays, but, I need to be able to send the 5 judge scores into a function and have it determine the highest score (another function will find the lowest). I'm at a lost on how to validate the data into finding the highest number. Any suggestions?
Easy way is declare that judges score as global variable, so you don't need to pass it via parameter

Or create static var insde min max function uh.. like this
1
2
3
4
5
6
7
8
9
10
11
int min(int grade){
  static int judge1, judge2, judge3;
  if(judge1==0)judge1=grade;else
  if(judge2==0)judge2=grade;else
  judge3=grade;
  if(judge1!=0&&judge2!=0&&judge3!=0){
    //find the mininum
    judge1=judge2=judge3=0; //clear the variable
    //return min to main
  }else return 0;
}

Its not very effectve anyway

Or create a function with 5 parameter
This here will find the max in the array
1
2
3
4
5
6
7
8
9
10
int findHighest (int array[])
{
   max = number[0];
   for (i = 0; i < sizeof(array); i++)
   {
	if (max < number[i])
		max = number[i];
   }
   return max;
}

Topic archived. No new replies allowed.