"Lowest Score Drop" hw problem

Hi all! I bet this place fills up this time of day on Sunday.

Anyhow, I am doing the "Drop the lowest score" beginner C++ program, instructions are:

Write a program that calculates the average of a group of test scores, where the lowest score in the group is dropped. It should use the following functions:
- void getScore() should ask the user for a score, store it in a reference parameter variable, and validate it. This function should be called by main once for each of the five scores to be entered.
-void calcAverage() should calculate and display the average of the four highest scores. This function should be called just once by main, and should be passed the five scores.
-int findLowest() should find and return the lowest of the five scores passed to it. It should be called by calcAverage, which uses the function to determine which of the five scores to drop.

Input validation: do not accept scores lower than 0 or higher than 100.


So basically, I know this could be written more simply but we are learning functions. Also, arrays have not yet been taught (next chapter, so I can't use them here). Here's my error:

2 IntelliSense: more than one instance of overloaded function "getScore" matches the argument list:
function "getScore(int)"
function "getScore(int &score)"
argument types are: (int) c:\Users\Owner\Documents\Visual Studio 2013\Projects\ConsoleApplication45\ConsoleApplication45\Source.cpp 18 2 ConsoleApplication45

And here is the code. Any help is greatly appreciated. Thank you.

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

void getScore(int);
void calcAverage(int, int, int, int, int, int);
int findLowest(int, int, int, int, int);

int main(){

	int score1, score2, score3, score4, score5, lowest;

	getScore(score1);
	getScore(score2);
	getScore(score3);
	getScore(score4);
	getScore(score5);

	findLowest(score1, score2, score3, score4, score5);

	calcAverage(score1, score2, score3, score4, score5, lowest);

	system("pause");
	return 0;
}

void getScore(int &score){

	cout << "Please enter a score between 0 - 100: ";
	cin >> score;

	while (score < 0 || score > 100){
		cout << "Error - enter a score between 0 - 100: ";
		cin >> score;
	}
}

int findLowest(int score1, int score2, int score3, int score4, int score5, int lowest){
	int lowest = score1;

	if (score2 < lowest)
		lowest = score2;
	else if (score3 < lowest)
		lowest = score3;
	else if (score4 < lowest)
		lowest = score4;
	else if (score5 < lowest)
		lowest = score5;

	cout << "The lowest score is " << lowest << endl;
	return lowest;
}

void calcAverage(int score1, int score2, int score3, int score4, int score5){
	int findLowest(int, int, int, int, int, int);
	int lowest;
	double average;

	findLowest(score1, score2, score3, score4, score5, lowest);

	average = (((float)score1 + score2 + score3 + score4 + score5) - lowest) / 4.0;

	cout << setw(4);
	cout << fixed << showpoint << setprecision(2);
	cout << "The average of the 4 highest scores is " << average << endl;
}
Your prototype does not match the function definition.

1
2
3
void getScore(int); 

void getScore(int&); // <--- Should look liket his with & 
Thank you so much! I didn't realize the & went both places.

I'm now getting another error...

Error 1 error C2082: redefinition of formal parameter 'lowest' e:\beginning programming\chapter 6\chapter 6 prog chal 10.cpp 44 1 ConsoleApplication47
closed account (18hRX9L8)
@Andromeda441: You are redefining lowest in findLowest.

Your homework assignment only wants you to send the five scores to findLowest. Therefore, I would change its declaration to int findLowest(int score1, int score2, int score3, int score4, int score5); and call it like: lowest = findLowest(score1, score2, score3, score4, score5);.

Next, your findLowest is not going to output the correct lowest score because the conditional block breaks when a condition is met, effectively skipping checking the other numbers. To fix this, you need to remove the else if's and replace them with if's so each check is its own block, allowing you to check all the numbers against lowest.
Last edited on
Thank you so much, TarikNeaj and usandfriends!! I really appreciate the help. I am working hard to really understand this. Thanks again for taking the time to help me. Going to go follow your advice, usandfriends, wish me luck!
closed account (18hRX9L8)
Sure, if any of my explanations didn't make any sense, let me know. Also don't forget to update line 55 (int findLowest(int, int, int, int, int, int);).
Last edited on
One more question...if I call it like:
 
lowest = findLowest(score1, score2, score3, score4, score5);


then I can't use:

1
2
3
4
lowest = score1

if (score2 < lowest)
lowest = score2;


etc...right? I'm still getting the error about the redefinition of a formal parameter.

I totally see what you mean about the if statement. I obviously didn't put enough thought into that one. Thanks!!
closed account (18hRX9L8)
I was looking at your code and realized I was reading calcAverage as main. Sorry about that.

Let me go over your code once again now that I'm reading it right:

usandfriends wrote:
You are redefining lowest in findLowest.

Your homework assignment only wants you to send the five scores to findLowest. Therefore, I would change its declaration to int findLowest(int score1, int score2, int score3, int score4, int score5); and call it like: lowest = findLowest(score1, score2, score3, score4, score5);.

Next, your findLowest is not going to output the correct lowest score because the conditional block breaks when a condition is met, effectively skipping checking the other numbers. To fix this, you need to remove the else if's and replace them with if's so each check is its own block, allowing you to check all the numbers against lowest.

This is all still correct.

You can remove the line where you define findLowest in calcAverage as findLowest is already defined globally. (Is this where you were getting your redefinition error?)

Same for findLowest, calcAverage only takes in the five numbers. Remove int lowest from the parameters and call it like calcAverage(score1, score2, score3, score4, score5) in main.

In main, you don't have to define lowest or call getLowest as you only need the lowest number in calcAverage. You should only be calling getLowest once: in calcAverage.

To address your question, you would define lowest as int lowest = score1; in findLowest because it isn't sent as a parameter. It should not give you the redefinition error because you have not defined it before (or sent it as a parameter) and you are only defining it once in findLowest.
Last edited on
I'm trying to make the adjustments as you suggest, but by the number of errors it appears that I'm just making it worse. Here are the errors:

Warning 1 warning C4101: 'lowest' : unreferenced local variable e:\beginning programming\chapter 6\chapter 6 prog chal 10.cpp 16 1 ConsoleApplication49
Error 2 error C2082: redefinition of formal parameter 'lowest' e:\beginning programming\chapter 6\chapter 6 prog chal 10.cpp 48 1 ConsoleApplication49


Here's the 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
#include<iostream>
#include<iomanip>
using namespace std;

void getScore(int&);
void calcAverage(int, int, int, int, int);
int findLowest(int score1, int score2, int score3, int score4, int score5);

int main(){

	int score1, score2, score3, score4, score5, lowest;

	getScore(score1);
	getScore(score2);
	getScore(score3);
	getScore(score4);
	getScore(score5);

	calcAverage(score1, score2, score3, score4, score5);

	double average;

	cout << setw(4);
	cout << fixed << showpoint << setprecision(2);
	cout << "The average of the 4 highest scores is " << average << endl;

	system("pause");
	return 0;
}

void getScore(int &score){

	cout << "Please enter a score between 0 - 100: ";
	cin >> score;

	while (score < 0 || score > 100){
		cout << "Error - enter a score between 0 - 100: ";
		cin >> score;
	}
}

int findLowest(int score1, int score2, int score3, int score4, int score5, int lowest){
	int lowest = score1;

	if (score2 < lowest)
		lowest = score2;
	if (score3 < lowest)
		lowest = score3;
	if (score4 < lowest)
		lowest = score4;
	if (score5 < lowest)
		lowest = score5;

	cout << "The lowest score is " << lowest << endl;
	return lowest;
}

void calcAverage(int score1, int score2, int score3, int score4, int score5){
	int lowest;
	double average;

	lowest = findLowest(score1, score2, score3, score4, score5);

	average = (((float)score1 + score2 + score3 + score4 + score5) - lowest) / 4.0;

	
}


Thanks again.
1
2
int findLowest(int score1, int score2, int score3, int score4, int score5, int lowest){
	int lowest = score1;


Its probably becuase, you have named two variables the same, the compiler gets hella confused you know?
Well, I realize that, I just don't know how to fix it. Now I'm getting multiple errors. I'll start over with the original code that I posted above.
closed account (18hRX9L8)
You still haven't updated parts of the program...

Main should look something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main(){
	int score1, score2, score3, score4, score5;

	getScore(score1);
	getScore(score2);
	getScore(score3);
	getScore(score4);
	getScore(score5);

	// We don't need lowest or average variables here, calcAverage will do all that work...
	calcAverage(score1, score2, score3, score4, score5);

	system("pause");
	return 0;
}


calcAverage should look like:

1
2
3
4
5
6
7
8
9
10
void calcAverage(int score1, int score2, int score3, int score4, int score5){
	// Find lowest and average.
	int lowest = findLowest(score1, score2, score3, score4, score5);
	double average = (((float)score1 + score2 + score3 + score4 + score5) - lowest) / 4.0;

	// Print them out..
	cout << setw(4);
	cout << fixed << showpoint << setprecision(2);
	cout << "The average of the 4 highest scores is " << average << endl;
}


Line 42, you haven't updated all the declarations, only the top ones.
Should be: int findLowest(int score1, int score2, int score3, int score4, int score5){
Last edited on
Topic archived. No new replies allowed.