Function Program Help

I was instructed to create a program that finds the winner of a talent contest. The contest has five judges, each of whom awards a score between 1 and 10
for each of the performers. Fractional scores are not allowed. A contestant’s final score is determined by dropping the highest and lowest scores received,
then averaging the three remaining scores (the average should be rounded to two decimal places). The winner is the contestant with the highest average score.
If there is a tie, the winner will be the first contestant judged.

I already know that my code so far does not make any sense and that it won't compile because some variables aren't defined. This is my problem! I need to figure out a way to have each score be saved and not changed after every loop. I can't think of a way to define each score as score1, score2, score3, etc.

Here are my requirements:
1. Input the contestant’s first name followed by the 5 judges’ scores. You do not know how many contestants there are. Design the loop so the loop terminates when you are finished entering contestants.
2. Input validation: Do not accept a judge’s score that is less than 1 or greater than 10. As each score is entered send the score to a function to test the score’s validity.
3. Use function calcAvgScore that has the contestant’s 5 scores as input parameters
a. returns the average score for that contestant.
b. Calls two functions, findLowest and findHighest which both accept the 5 scores as input parameters and return the lowest and highest scores, respectively.
4. Do not use global variables. All variables used in the functions must be passed as parameters or declared locally.
5. At the end of the program, display the winner and winning score (rounded to 2 decimal places).


Here is my code so far:
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <string>
#include <iomanip>
using namespace std; 

//Function prototypes
void testScore(int);
double findLowest(double, double, double, double, double);
double findHighest(double, double, double, double, double);
double calcAvgScore (double, double, double, double, double);

int main()
{
int score;
string name;
string name;


cout << "Enter the name of the star, enter Done if there are no stars\n";
cin>> name;

while(name != "Done" && name !="done") 
{
	
	for (int i = 0; i < 5; i++) 
	{
	   cout << "Enter judge " << i + 1 << " score ";
	   cin >> score; 
	   testScore(score); 

	}
	cout << "Enter the name of the star, enter Done if there are no stars\n";
	cin>> name;
}
double average = calcAvgScore(score1, score2, score3, score4, score5);
cout << "...And the winner is " << name << " with an average of " << average << endl;  

	

	system ("pause");
	return 0;
}


//****************************************************************************
// Definition of the function testScore which is input validation for scores *
//****************************************************************************

void testScore(int score)
{
	if (score < 1 || score > 10)
	{
		cout << "Invalid score, please enter a score 1-10 ";
		cin >> score;
		testScore(score);
	}
	
}
//***********************************************************************
// Definition of the function findLowest which finds the lowest score   *
//***********************************************************************   

double findLowest(double score1, double score2, double score3, double score4, double score5)
   {
       double lowest = 0;
       if (score1 > lowest)
           lowest = score1;
       if (score2 > lowest)
           lowest = score2;
       if (score3 > lowest)
           lowest = score3;
       if (score4 > lowest)
           lowest = score4;
       if (score5 > lowest)
           lowest = score5;

       return lowest;
   }

//***********************************************************************
// Definition of the function findHighest which finds the highest score *
//***********************************************************************   

   double findHighest(double score1, double score2, double score3, double score4, double score5)
   {
       double highest = 0;
       if (score1 > highest)
           highest = score1;
       if (score2 > highest)
           highest = score2;
       if (score3 > highest)
           highest = score3;
       if (score4 > highest)
           highest = score4;
       if (score5 > highest)
           highest = score5;

       return highest;
   }
//***********************************************************************
// Definition of the function calcAvgScore which calculates the average *
//***********************************************************************

double calcAvgScore (int score1, int score2, int score3, int score4, int score5)
{
	 double sum = (score1 + score2 + score3 + score4 + score5) -(findHighest(score1, score2, score3, score4, score5) + findLowest(score1, score2, score3, score4, score5));
	 double average = (sum / 3.0);
     return average;
}
Last edited on
I think it's enough if you save only one name and one score. In your loop you get the name and the 5 scores. When you have calculated the average you compare it with the previous saved score. If it is higher than you save the score and name.

The easiest would be to store the scores in an array - if allowed - and pass this array to the functions.
An array would be helpful, but sadly I am not allowed to use an array in this program.

Do you think it would help to get rid of the for loop since the number of judges is a set number (5)?

I also don't know if adding score1 += score;, score2 += score;, etc. to each score submission will add that value into the variable score1, score2, score3, etc.?

If I take away the for loop, this is what my code looks like now:

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134

#include <iostream>
#include <string>
#include <iomanip>
using namespace std; 

//Function prototypes
void testScore(int);
double findLowest(double, double, double, double, double);
double findHighest(double, double, double, double, double);
double calcAvgScore (double, double, double, double, double);

int main()
{
int score;
int score1 = 0,
	score2 = 0, 
	score3 = 0, 
	score4 = 0, 
	score5 = 0;
string name;



cout << "Enter the name of the star, enter Done if there are no stars\n";
cin>> name;

while(name != "Done" && name !="done") 
{
	
	   cout << "Enter judge 1 score ";
	   cin >> score;
	   testScore(score);  
	   score1 += score;

	   cout << "Enter judge 2 score ";
	   cin >> score;
	   testScore(score);  
	   score2 += score;

	   cout << "Enter judge 3 score ";
	   cin >> score;
	   testScore(score);  
	   score3 += score;

	   cout << "Enter judge 4 score ";
	   cin >> score;
	   testScore(score);  
	   score4 += score;

	   cout << "Enter judge 5 score ";
	   cin >> score;
	   testScore(score);  
	   score5 += score;

	cout << "Enter the name of the star, enter Done if there are no stars\n";
	cin>> name;
	 
}



	

	system ("pause");
	return 0;
}


//****************************************************************************
// Definition of the function testScore which is input validation for scores *
//****************************************************************************

void testScore(int score)
{
	if (score < 1 || score > 10)
	{
		cout << "Invalid score, please enter a score 1-10 now: ";
		cin >> score;
		testScore(score);
	}
	
}
//***********************************************************************
// Definition of the function findLowest which finds the lowest score   *
//***********************************************************************   

double findLowest(int score1, int score2, int score3, int score4, int score5)
   {
       double lowest = 0;
       if (score1 > lowest)
           lowest = score1;
       if (score2 > lowest)
           lowest = score2;
       if (score3 > lowest)
           lowest = score3;
       if (score4 > lowest)
           lowest = score4;
       if (score5 > lowest)
           lowest = score5;

       return lowest;
   }

//***********************************************************************
// Definition of the function findHighest which finds the highest score *
//***********************************************************************   

   double findHighest(int score1, int score2, int score3, int score4, int score5)
   {
       double highest = 0;
       if (score1 > highest)
           highest = score1;
       if (score2 > highest)
           highest = score2;
       if (score3 > highest)
           highest = score3;
       if (score4 > highest)
           highest = score4;
       if (score5 > highest)
           highest = score5;

       return highest;
   }
//***********************************************************************
// Definition of the function calcAvgScore which calculates the average *
//***********************************************************************

double calcAvgScore (int score1, int score2, int score3, int score4, int score5)
{
	 double sum = (score1 + score2 + score3 + score4 + score5) -(findHighest(score1, score2, score3, score4, score5) + findLowest(score1, score2, score3, score4, score5));
	 double average = (sum / 3.0);
     return average;
}


If this is acceptable and the scores actually store in each of their respected variable, would this work to produce the outcome I'm looking for?
Topic archived. No new replies allowed.