Drop lowest score

Hello,
I am having issues with my program. I have to get six test score from the user and the scores have to be in between 0-100 and it has to drop the lowest test score then calculate the average. When I compile it states no issues, then I run it and it said test6-test1 are not initialized and I'm not sure how to fix it. Any help would be appreciated.

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

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

int main()
{
	static int average, test1, test2, test3, test4, test5, test6;
	getScore (test1, test2, test3, test4, test5, test6);
	calcAverage (average, test1, test2, test3, test4, test5, test6);
}
	void getScore (int test1, int test2, int test3, int test4, int test5, int test6)
	{
		if (test1, test2, test3, test4, test5, test6 < 0 && test1, test2, test3, test4, test5, test6 > 100)
			cout << "Invaild test score. Please enter a score between 0 and 100.";
		cout << "Enter first score: ";
		cin >> test1;
		cout << "Enter second score: ";
		cin >> test2;
		cout << "Enter third score: ";
		cin >> test3;
		cout << "Enter fourth score: ";
		cin >> test4;
		cout << "Enter fifth score: ";
		cin >> test5;
		cout << "Enter sixth score: ";
		cin >> test6;
	}
	void calcAverage( int average, int test1, int test2, int test3, int test4, int test5, int test6)
	{
		int small = findLowest(test1, test2, test3, test4, test5, test6);
			average = (((test1 + test2 + test3 + test4 + test5 + test6) - small) / 5);
			cout << small << endl;
			cout << average;
			system("pause");
			
	}
	int findLowest (int test1, int test2, int test3, int test4, int test5, int test6)
	{
		int small = test1;
		if(small > test2)
		{
			small = test2;
		}
		if(small > test3)
		{
			small = test3;
		}
		if(small > test4)
		{
			small = test4;
		}
		if(small > test5)
		{
			small = test5;
		}
		if(small > test6)
		{
			small = test6;
		}
	return small; 
	}
1
2
3
4
5
6
7
8
9
10
11
	void getScore (int test1, int test2, int test3, int test4, int test5, int test6)
	{
		if (test1, test2, test3, test4, test5, test6 < 0 && test1, test2, test3, test4, test5, test6 > 100)
			cout << "Invaild test score. Please enter a score between 0 and 100.";
		cout << "Enter first score: ";
		cin >> test1;
		cout << "Enter second score: ";
		cin >> test2;
                ...
	}
	}

In your getScore function, the passed arguments (test1, test2, test3...) are local only to that function. It does not modify the called values outside of the function unless you use references!

Here is an example to compare, using references:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void foo(int arg)
{
    arg = 42;
}

void foo2(int& arg)
{
    arg = 42;
}

int main()
{
    int a = -1;
    foo(a);
    std::cout << a << std::endl; //still -1
    foo2(a);
    std::cout << a << std::endl; // 42 now, foo2 changes a
}




Also:
if (test1, test2, test3, test4, test5, test6 < 0 && test1, test2, test3, test4, test5, test6 > 100)
You can't do a comma-separated boolean statement like that, you'd need to test each one. Also, I think you'd want to be using OR, not AND.
if (test1 < 0 || test2 < 0 .... || test1 > 100 || test2 > 100 ... )

But! you should never have to do something so tedious for something so simple. I guess you're just beginning and haven't learned about it yet, but std::vector<int> could store a dynamic list of test grades, not just a hard-coded number like 6, so that you could easily delete the lowest grade from the vector. Or if you know you're dealing with 6 grades, have an int grades[6] array.

something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
    int grades[6];
    for (int i = 0; i < 6; i++)
    {
        std::cin >> grades[i];
    }

    int total = 0;
    for (int i = 0; i < 6; i++)
    {
        total += grades[i];
    }
    double avg = total/6.0;
}

Doesn't take into account to drop the lowest grade, but hopefully you can see what's happening.

(Or just ignore that long boolean statement for now, and just don't enter negative/over-100 values.)
Last edited on
Yes I am very new at this and I forgot to put that it only states those test6-test1 are not initialized if I don't have static int. Also if I put in std::vector<int> I wont have to put test1-test6?
Sorry I edited my post, the first part of my edited post relates to the initialization problem.

Basically, your getScore function never actually modifies the values declared in your main function.

Here's working code, I think, but try to understand the example code in the previous post about references in functions.
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&, int&, int&, int&, int&, int&);
void calcAverage(int, int, int, int, int, int, int);
int findLowest(int, int, int, int, int, int);

int main()
{
	int average, test1, test2, test3, test4, test5, test6;
	getScore (test1, test2, test3, test4, test5, test6);
	calcAverage (average, test1, test2, test3, test4, test5, test6);
	
	char a;
	cin >> a; //in case your environment closes the window...
	return 0;
}
	void getScore (int& test1, int& test2, int& test3, int& test4, int& test5, int& test6)
	{
		cout << "Enter first score: ";
		cin >> test1;
		cout << "Enter second score: ";
		cin >> test2;
		cout << "Enter third score: ";
		cin >> test3;
		cout << "Enter fourth score: ";
		cin >> test4;
		cout << "Enter fifth score: ";
		cin >> test5;
		cout << "Enter sixth score: ";
		cin >> test6;
	}
	void calcAverage( int average, int test1, int test2, int test3, int test4, int test5, int test6)
	{
		int small = findLowest(test1, test2, test3, test4, test5, test6);
			average = (((test1 + test2 + test3 + test4 + test5 + test6) - small) / 5);
			cout << small << endl;
			cout << average;
			
	}
	int findLowest (int test1, int test2, int test3, int test4, int test5, int test6)
	{
		int small = test1;
		if(small > test2)
		{
			small = test2;
		}
		if(small > test3)
		{
			small = test3;
		}
		if(small > test4)
		{
			small = test4;
		}
		if(small > test5)
		{
			small = test5;
		}
		if(small > test6)
		{
			small = test6;
		}
	return small; 
	}



Another thing, there's no point in passing an "average" argument into your calcAverage function, since you don't do anything with that value. It should be a local value you return instead.

Also you shouldn't need to worry about using the static keyword for this exercise, it should work without it.
Last edited on
Here is the program without arrays, but with a loop. Let me know if you have any questions; I tried to comment it well.

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
#include <iostream>

using namespace std;


const int N = 6; //how many grades to ask for

int main() {
	double sum = 0; //holds the running total
	double lowest = 100; //holds the lowest score so far
	
	//ask for the N scores
	for( int i = 0; i < N; i++ ) {
		double score; //holds each input
		cout << "Enter score " << i + 1 << ": ";
		cin >> score;
		if( score >= 0 && score <= 100 ) {
			//the input was valid
			sum += score; //add it to the running total
			if( score < lowest )
				lowest = score; //update the lowest score if this score is lower 
		} else {
			cout << "Invalid score!\n"; //let them know the input was invalid
			i--; //make them re-enter the score
		}
	}
	
	sum -= lowest; //drop the lowest score
	double average = sum / (N - 1); //N - 1 because we droped the lowest score
	
	cout << "The average score (after dropping the lowest score of " << lowest
	     << ") was " << average << endl;
}
@Ganado what if i input a negative score on your program?
its still computing. read the OP's exercise
I stated that I ignored that long if statement part for now because it was too tedious to type out, Mathhead does it more neatly anyway.
Last edited on
I have the programing working now. The only issue I am having is trying to get it to it to ask for a certain test score again if the user does not but in the right value. Right now if I input a value over 100 or below 0 it saids what I need it to but will not ask the question again. Can anybody help me with that please and thank you.


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

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

int main()
{
    float test1, test2, test3, test4, test5, test6;
	getScore (test1, test2, test3, test4, test5, test6);
	calcAverage (test1, test2, test3, test4, test5, test6);
}
void getScore (float& test1, float& test2, float& test3, float& test4, float& test5, float& test6)
{
	cout << "Enter first score: ";
	cin >> test1;
	if (test1 < 0 || test1 > 100)
		cout << "Invaild test score. Please enter a score between 0 and 100.\n";
		
	cout << "Enter second score: ";
	cin >> test2;
	if (test2 < 0 || test2 > 100)
		cout << "Invaild test score. Please enter a score between 0 and 100.\n";
		
	cout << "Enter third score: ";
	cin >> test3;
	if (test3 < 0 || test3 > 100)
		cout << "Invaild test score. Please enter a score between 0 and 100.\n";
		
	cout << "Enter fourth score: ";
	cin >> test4;
	if (test4 < 0 || test4 > 100)
		cout << "Invaild test score. Please enter a score between 0 and 100.\n";
		
	cout << "Enter fifth score: ";
	cin	>> test5;
	if (test5 < 0 || test5 > 100)
		cout << "Invaild test score. Please enter a score between 0 and 100.\n";
		
	cout << "Enter sixth score: ";
	cin >> test6;
	if (test6 < 0 || test6 > 100)
		cout << "Invaild test score. Please enter a score between 0 and 100.\n";
}
void calcAverage(float test1, float test2, float test3, float test4, float test5, float test6)
{
	float average, small = findLowest(test1, test2, test3, test4, test5, test6);
	average = (((test1 + test2 + test3 + test4 + test5 + test6) - small) / 5);
	cout << "Your lowest test score is " << small << endl;
	cout << "Your test score average is " << average << setprecision(3) << endl;
	system("pause");
}
int findLowest (float test1, float test2, float test3, float test4, float test5, float test6)
{
	float small = test1;
	if(small > test2)
	{
		small = test2;
	}
	if(small > test3)
	{
		small = test3;
	}
	if(small > test4)
	{
		small = test4;
	}
	if(small > test5)
	{
		small = test5;
	}
	if(small > test6)
	{
		small = test6;
	}
	return small; 
}
1
2
3
4
5
6
7
8
9
...
do
{
    cout << "Enter first score: ";
    cin >> test1;
    if (test1 < 0 || test1 > 100)
        cout << "Invaild test score. Please enter a score between 0 and 100.\n";
} while (test1 < 0 || test1 > 100);
...
Last edited on
Topic archived. No new replies allowed.