modular functions and nested looping?

For class i'm supposed to write a program that: asks for five scores, finds the min and max, and finds the average after subtracting the min and max. Its supposed to do this using modular functions, but i tried running it ad instead of giving me values it gave me a long string of numbers and characters, im not sure why i got this error, can anyone take a look and see what i did wrong? (ideally my findHighest, findLowest, and calcScore functions?)

edit: i was able to get them to run by putting the rest of the call... but now they arent giving the correct values...

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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
  #include <iostream>
#include <iomanip>

using namespace std;

void getJudgeData(double &score); 
//prototype for judge function

void calcScore(double s1, double s2, double s3, double s4, double s5);
//prototype to calculate average

double findLowest(double s1, double s2, double s3, double s4, double s5);
//protype to find lowest score

double findHighest(double s1, double s2, double s3, double s4, double s5);
//protype to find highest score


int main ()
{

	int contestant = 1;
	double score = 0;

	char yesNo = 'n'; //defines variable yesNo
	
	do
		{
			cout << "\nwelcome to star search" << "\n" << "----------------------" << endl; //introduction to program
			cout << "contestant #" << contestant << "\n" << endl;

			getJudgeData(score);  // calls to function getJudgeData
			

			cout << "would you like to enter another contestant? (Y/N)" << endl;
			cin >> yesNo; 

			contestant++; //inrements contestant

		} while (yesNo == 'y' || yesNo == 'Y'); //creates parameter


	system("pause");
	return 0;

}

void getJudgeData(double &score)
{
	
	double score1, score2, score3, score4, score5 = 0; //declares scores
	
	cout << "*****judge1*****" << endl;
	cout << "enter a score: (1-10)" << endl;
	cin >> score1;

		while (score1 < 0 || score1 >10) // prevents scores less than zero and more than ten
			{
				cout << "please enter a valid number: (1-10)" << endl;
				cin	>> score1;
			}

	cout << "\n*****judge2*****" << endl; // repeats for every judge
	cout << "enter a score: (1-10)" << endl;
	cin >> score2;

		while (score2 < 0 || score2 >10)
		{
			cout << "please enter a valid number: (1-10)" << endl;
			cin >> score2;
		}

	cout << "\n*****judge3*****" << endl;
	cout << "enter a score: (1-10)" << endl;
	cin >> score3;

		while (score3 < 0 || score3 >10)
		{
			cout << "please enter a valid number: (1-10)" << endl;
			cin >> score3;
		}

	cout << "\n*****judge4*****" << endl;
	cout << "enter a score: (1-10)" << endl;
	cin >> score4;

		while (score4 < 0 || score4 >10)
			{
				cout << "please enter a valid number: (1-10)" << endl;
				cin >> score4;
			}

	cout << "\n*****judge5*****" << endl;
	cout << "enter a score: (1-10)" << endl;
	cin >> score5;

		while (score5 < 0 || score5 >10)
			{
				cout << "please enter a valid number: (1-10)" << endl;
				cin >> score5;
			}

	
	findHighest(score1, score1, score3, score4, score5); //calls findHighest function
	findLowest (score1, score1, score3, score4, score5); //calls findLowest function

	cout << "\nhighest score is: " << findHighest << endl; //shows highest
	cout << "lowest score is: " << findLowest << endl; //shows lowest

	calcScore(score1, score1, score3, score4, score5); //calls to calcScore
	
	
}

void calcScore(double s1, double s2, double s3, double s4, double s5) //finds the final score
{
	double	score1 = s1;
	double	score2 = s2;
	double	score3 = s3;
	double	score4 = s4;
	double	score5 = s5;
	
	double highest = findHighest(score1, score1, score3, score4, score5); // calls to findHighest
	double lowest = findLowest(score1, score1, score3, score4, score5); // calls to findLowest

	double scoresAdded = (score1 + score2 + score3 + score4 + score5); // adds the scores

	double	finalScore = (scoresAdded  - highest - lowest)/3; // finds the average after cutting the highest and lowest scores


	cout << "\nthe final score is: " << calcScore << endl << endl; // shows final score


}

double findLowest(double s1, double s2, double s3, double s4, double s5) //finds the min
{
	double lowest = 0; // declares lowest as min

	if (s1 < s2 && s1 < s3 && s1 < s4 && s1 < s5) //declares s1 as min
		{
			lowest = s1;
		}
	if (s2 < s1 && s2 < s3 && s2 < s4 && s2 < s5) // s2 as min
		{
			lowest = s2;
		}
	if (s3 < s2 && s3 < s1 && s3 < s4 && s3 < s5) // s3 as min
		{
			lowest = s3;
		}
	if (s4 < s2 && s4 < s3 && s4 < s1 && s4 < s5) // s4 as min
		{
			lowest = s4;
		}
	if (s5 < s2 && s5 < s3 && s5 < s4 && s5 < s1) // s5 as min
		{
			lowest = s5;
		}

	return lowest; // returns lowest as the value of findLowest
}

double findHighest(double s1, double s2, double s3, double s4, double s5) //finds max
{
	
	double highest = 0; //declares highest as max

	if (s1 > s2 && s1 > s3 && s1 > s4 && s1 >s5 ) //declares s1 as max
		{
			highest = s1;
		}
	if (s2 > s1 && s2 > s3 && s2 > s4 && s2 >s5) // s2 as max
		{
			highest = s2;
		}
	if (s3 > s2 && s3 > s1 && s3 > s4 && s3 >s5) // s3 as max
		{
			highest = s3;
		}
	if (s4 > s2 && s4 > s3 && s4 > s1 && s4 >s5) // s4 as max
		{
			highest = s4;
		}
	if (s5 > s2 && s5 > s3 && s5 > s4 && s5 >s1) // and s5 as max
		{
			highest = s5;
		}

	return highest; //returns highest as the value of findHighest

}
Last edited on
In your functions
findLowest()
and findHighest() a value of type double is returned.

In getJudgeData() where the functions are called, nothing is done with that result, it is simply discarded. Later, in the cout statement,
 
    cout << "\nhighest score is: " << findHighest << endl;
rather than the resulting value being output, instead it is the address of the function which will be shown.

Try it like this:
1
2
3
    // Store value returned by function in variable highest, and output it.
    double highest = findHighest(score1, score1, score3, score4, score5);
    cout << "\nhighest score is: " << highest << endl;


Other comments.

Passing large numbers of similar parameters rapidly grows cumbersome. It would be more concise to use an array.

in function getJudgeData()
1
2
    // declare the array
    double scores[5];

Calling the function:
1
2
    // pass the array as a function parameter
    double highest = findHighest(scores);


The function itself:
1
2
3
4
5
6
7
8
9
10
11
12
13
double findHighest(double scores[]) 
{
    double highest = scores[0]; // Store first element as max

    // Loop through remaining four values in the array
    for (int i=1; i<5; ++i)
    {
        if (scores[i] > highest)
            highest = scores[i];
    }

    return highest;
}


Even if you don't do any of that, there is another simplification which could be done.
Function getJudgeData() contains a lot of repetition. It is really a good case for the use of a loop. That would be simple if an array is used. But an alternative would be to take the repetitive code such as:
1
2
3
4
5
6
7
8
9
    cout << "\n*****judge2*****" << endl; // repeats for every judge
    cout << "enter a score: (1-10)" << endl;
    cin >> score2;

    while (score2 < 0 || score2 >10)
    {
        cout << "please enter a valid number: (1-10)" << endl;
        cin >> score2;
    }

and that could be placed in a function of its own. Only the text "judge2" and the variable score2 changes each time. So you could pass an integer (the number 2) as a function parameter, and return the score from the function.

Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
double getScore(int n)
{      
    cout << "\n*****judge" << n << "*****" << endl;
    cout << "enter a score: (1-10)" << endl;
    
    double score;
    cin >> score;

    while (score < 0 || score > 10)
    {
        cout << "please enter a valid number: (1-10)" << endl;
        cin >> score;
    }
    
    return score;
}


... or something like that.

That would be useable whether you use five separate variables, or an array as suggested earlier.
Last edited on
thanks, i got it figured out in a really overly complex way though XD, this helps alot
Topic archived. No new replies allowed.