Help with Functions and Data Types?

In this program, I had to create a talent show scoring system. 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.

My problem is how do I limit the program to just display the winners name?
And what kind of code do I need to include if there is a tie in the scores?
I'm also having an issue where the average isn't producing the right number. For example if I input:
score 1: 10
score 2: 9
score 3: 8
score 4: 9
score 5: 10

I get an average of 9, not 9.33 like I want. I know it has something to do with the data type (int and double) but I can't quite figure out where I'm going wrong with the type.


Here is my code:
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

// Function prototypes

double testScore(int);   // input validation for scores
double findHighest(int, int, int, int, int); // Find highest score of each star
double findLowest(int, int, int, int, int); // Find lowest score of each star
double calcAvgScore (double, double, double, double, double); // calculate the average score

int main()
{

double score1, score2, score3, score4, score5;
double highest, lowest;

double score, winnerScore;
string name, winnerName;


cout << "Welcome to The Talent Show!\n";

// Get star's name, terminate loop when done is entered
do
{
	cout << "Enter the name of the star, enter Done if there are no stars\n";
	cin >> name;
	cout << endl;

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


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

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

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

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

	// Output scores for validation 

	cout << "Score 1 " << score1 << endl;
	cout << "Score 2 " << score2 << endl;
	cout << "Score 3 " << score3 << endl;
 	cout << "Score 4 " << score4 << endl;
	cout << "Score 5 " << score5 << endl;

     }

} while (name != "Done" && name !="done");
// Call average score calculation function
double average = calcAvgScore(score1, score2, score3, score4, score5);





//Output winner with top average
cout << "...and the winner is " << name;

// set up numeric out put formatting
cout << fixed << showpoint << setprecision(2);
cout << " with a score of " << average << "!" << endl;



system("pause");
return 0;
}


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

double testScore(int score)
{
	while (score<1 || score>10)
{
	cout << "Please enter a valid score between 1 and 10: " ;
	cin >> score;
}
return score;
}

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

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

return highest;
}

//***********************************************************************
// Definition of the function findLowest which finds the lowest score   *
//***********************************************************************   

double findLowest(int score1, int score2, int score3, int score4, int score5)
{ 
	int lowest=10;
if (score1<lowest)
	lowest=score1;
else if (score2<lowest)
	lowest=score2;
else if (score3<lowest)
	lowest=score3;
else if (score4<lowest)
	lowest=score4;
else if (score5<lowest)
	lowest=score5;
 
return lowest;
}
//***********************************************************************
// Definition of the function calcAvgScore which calculates the average *
//***********************************************************************


double calcAvgScore (double score1, double score2, double score3, double score4, double 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 get an average of 9, not 9.33 like I want
your find{Highest,Lowest} are incorrect. You put else statements, that means that as soon as one condition is satisfied (by instance, that score1>highest) you'll stop checking, so you won't check the 5 numbers.


> My problem is how do I limit the program to just display the winners name?
I don't understand. If you don't want to display the score, then don't.
Thank you ne555. I changed the statements to just if statements and I'm getting the right results!!

And my instructions say that I specifically have to have the winners name and average displayed and I can't quite figure out how to have the winners name saved with their average.
Let's simplify.
The user will input numbers, ending with 0. output the largest number inputted by the user.
This is my sample output:
Enter the name of the star, enter done if there are no more stars
George
Enter judge 1 score 6
Enter judge 2 score 7
Enter judge 3 score 8
Enter judge 4 score 6
Enter judge 5 score 7

Enter the name of the star, enter done if there are no more stars
Martha
Enter judge 1 score 10
Enter judge 2 score 9
Enter judge 3 score 8
Enter judge 4 score 9
Enter judge 5 score 10

Enter the name of the star, enter done if there are no more stars
Leela
Enter judge 1 score 7
Enter judge 2 score 9
Enter judge 3 score 8
Enter judge 4 score 9
Enter judge 5 score 8

Enter the name of the star, enter done if there are no more stars
done

...and the winner is Martha with a score of 9.33!

Last edited on
Right now, the program isn't taking the highest average and displaying the output, it is simply taking the last scores entered, averaging them, and displaying that average. So I have to add something to just keep the highest average.
Maybe I'll have to add an if statement somewhere?
For example:
1
2
3
4
5
6
7
8
9
10
11
12
13

double average = 0.0;

average = calcAvgScore(score1, score2, score3, score4, score5);

if (average >= average){

    winnerName = name;

    winnerScore += average; 

}

Topic archived. No new replies allowed.