I need help with my code. Average Test score drop

Hello, I have a program that I am getting stuck with. I need help on finding the lowest test score and also validating user inputs because my isn't working.
Directions:

•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 test score, store it in a reference
parameter variable, and validate that it is not lower than 0 or higher than 100. 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 one
of the five scores to drop.

What I have 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

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

// Function prototype.
void getScore(int score1, int score2, int score3, int score4, int score5); // Gets user inputs
int findLowest(int score1, int score2, int score3, int score4, int score5, int lowest); // Finds the lowest score & drops it
void calcAverage(int score1, int score2, int score3, int score4, int score5); // Calculates and displays the average score.
int score1, score2, score3,score4, score5; // Defines integers used for the scores

void getScore(int score1, int score2, int score3, int score4, int score5)  // Gets the test scores from user inputs
{
    
    cout << "Enter score #1: "; // Asks for test score #1
    cin >> score1;
    if ((score1 > 100) || (score1 < 0))
    {
        cout << "Please enter a score between 0 and 100: ";
        cin >> score1;
    }
    
    cout << "Enter score #2: "; // Asks for test score #1
    cin >> score2;
    
    if ((score2 > 100) || (score2 < 0))
    {
        cout << "Please enter a score between 0 and 100: ";
        cin >> score2;
    }
    
    cout << "Enter score #3: "; // Asks for test score #1
    cin >> score3;
    
    if ((score3 > 100) || (score3 < 0))
    {
        cout <<  "Please enter a score between 0 and 100: ";
        cin >> score3;
    }
    
    cout << "Enter score #4: "; // Asks for test score #1
    cin >> score4;
    
    if ((score4 > 100) || (score4 < 0))
    {
        cout <<  "Please enter a score between 0 and 100: ";
        cin >> score4;
    }
    
    cout << "Enter score #5: "; // Asks for test score #1
    cin >> score5;
    
    if ((score5 > 100) || (score5 < 0))
    {
        cout << "Please enter a score between 0 and 100: ";
        cin >> score5;
    }
    
}

int findLowest(int score1, int score2, int score3, int score4, int score5, int lowest)  // Function that finds and drops the lowest score
{
    if (score2 < lowest) lowest = score2;
    if (score3 < lowest) lowest = score3;
    if (score4 < lowest) lowest = score4;
    if (score5 < lowest) lowest = score5;
    return lowest;
}

void calcAverage (int score1, int score2, int score3, int score4, int score5) // Function that calcualtes and displays the average test score.

{ double average;
    int lowest = findLowest(score1,score2,score3,score4,score5,lowest);
    average = (( score1 + score2 + score3 + score4 + score5) - lowest) / 4.0;
    cout << "The average of the 4 highest test scores is: " << average << endl;
    
}

int main()
{
    int score1, score2, score3,score4, score5;
    
    cout << "This program will average 5 test scores, with the lowest dropped, ";
    cout << "Please enter scores to calculate the average: \n";
    getScore(score1,score2,score3,score4,score5); // Calls function getScore
    calcAverage(score1,score2,score3,score4,score5); //Calls function calcAverage
    return 0;
}

Last edited on
By default, arguments to functions are passed by value. This means that a copy of the argument is dealt with in the function. Changing those copies has no effect on the original argument. Pass by reference if you wish to affect the original argument.

When calling functions that take arguments, you must supply those arguments to the function.

Line 67, for instance should be getScore(score1, score2...);.

Also, those scores should probably not be global variables. Stick 'em in main.
I have fixed a few things, and at one point the whole thing was working but the average was not coming out right. Now I have this and it doesn't work at all...
cire wrote:
By default, arguments to functions are passed by value. This means that a copy of the argument is dealt with in the function. Changing those copies has no effect on the original argument. Pass by reference if you wish to affect the original argument.

You haven't addressed this in your code.
Last edited on
I believe it is working now, would you mind putting it into your compiler and checking for me?

When I put it in Xcode it doesn't work, but when I use Ideone online, it works.. So I am not sure.

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

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

// Function prototype.
void getScore(int&, int&, int&, int&, int&); // Gets user inputs
int findLowest(int&, int&, int&, int&, int&, int&); // Finds the lowest score & drops it
void calcAverage(int&, int&, int&, int&, int&); // Calculates and displays the average score.

void getScore(int &score1, int &score2, int &score3, int &score4, int &score5)  // Gets the test scores from user inputs
{

	cout << "Enter score #1: "; // Asks for test score #1
	cin >> score1;
	if ((score1 > 100) || (score1 < 0))
	{
		cout << "Please enter a score between 0 and 100: ";
		cin >> score1;
	}

	cout << "Enter score #2: "; // Asks for test score #1
	cin >> score2;

	if ((score2 > 100) || (score2 < 0))
	{
		cout << "Please enter a score between 0 and 100: ";
		cin >> score2;
	}

	cout << "Enter score #3: "; // Asks for test score #1
	cin >> score3;

	if ((score3 > 100) || (score3 < 0))
	{
		cout << "Please enter a score between 0 and 100: ";
		cin >> score3;
	}

	cout << "Enter score #4: "; // Asks for test score #1
	cin >> score4;

	if ((score4 > 100) || (score4 < 0))
	{
		cout << "Please enter a score between 0 and 100: ";
		cin >> score4;
	}

	cout << "Enter score #5: "; // Asks for test score #1
	cin >> score5;

	if ((score5 > 100) || (score5 < 0))
	{
		cout << "Please enter a score between 0 and 100: ";
		cin >> score5;
	}

}

int findLowest(int &score1, int &score2, int &score3, int &score4, int &score5, int &lowest)  // Function that finds and drops the lowest score
{
	lowest = score1;
	if (score2 < lowest) lowest = score2;
	if (score3 < lowest) lowest = score3;
	if (score4 < lowest) lowest = score4;
	if (score5 < lowest) lowest = score5;
	return lowest;
}

void calcAverage(int &score1, int &score2, int &score3, int &score4, int &score5) // Function that calcualtes and displays the average test score.

{
	double average;
	int lowest = findLowest(score1, score2, score3, score4, score5, lowest);
	average = ((score1 + score2 + score3 + score4 + score5) - lowest) / 4.0;
	cout << "The average of the 4 highest test scores is: " << average << endl;

}

int main()
{
	int score1, score2, score3, score4, score5;

	cout << "This program will average 5 test scores, with the lowest dropped, ";
	cout << "Please enter scores to calculate the average: \n";
	getScore(score1, score2, score3, score4, score5); // Calls function getScore
	calcAverage(score1, score2, score3, score4, score5); //Calls function calcAverage
	return 0;
}
Topic archived. No new replies allowed.