Problems with functions and uninitialized local variables

Very new to programming, in a beginner's course. We're just starting to learn functions and were given an assignment to code as follows (use functions to get scores, drop the lowest score, and find the average with the lowest dropped). I've been scouring the web (and this site) for similar assignments and my code is close if not more accurate than what I am finding but I keep getting error codes for differing problems, here is my code as of now, not working. See below for the error codes I've got and what I tried to do to fix them.



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

void getScore(int userInput);
int calcAverage(int testScore1, int testScore2, int testScore3, int testScore4, int testScore5, int lowest, int avgMinusLowest);
int findLowest(int testScore1, int testScore2, int testScore3, int testScore4, int testScore5);

int main()
{
	int testScore1, testScore2, testScore3, testScore4, testScore5, lowest, average, avgMinusLowest;

	getScore(testScore1);
	getScore(testScore2);
	getScore(testScore3);
	getScore(testScore4);
	getScore(testScore5);

	lowest = findLowest(testScore1, testScore2, testScore3, testScore4, testScore5);
	cout << "The lowest score is: " << lowest << endl;

	average = calcAverage(testScore1, testScore2, testScore3, testScore4, testScore5, lowest, avgMinusLowest);
	cout << "The average is " << average << endl;
	
	return 0;
}

void getScore(int userInput)
{
	cout << "Enter a test score: ";
	cin >> userInput;

	while (userInput < 1 || userInput > 100)
	{
		cout << "Error! You must enter a value from 0-100. Try again: ";
		cin >> userInput;
	}
}

int calcAverage(int testScore1, int testScore2, int testScore3, int testScore4, int testScore5, int lowest, int avgMinusLowest)
{

	lowest = findLowest(testScore1, testScore2, testScore3, testScore4, testScore5);
	avgMinusLowest = ((testScore1 + testScore2 + testScore3 + testScore4 + testScore5 - lowest) / 5);

	return avgMinusLowest;
}

int findLowest(int testScore1, int testScore2, int testScore3, int testScore4, int testScore5)
{
	int lowest;
	lowest = testScore1;

	if (testScore2 < lowest)
		lowest = testScore2;
	else if (testScore3 < lowest)
		lowest = testScore3;
	else if (testScore4 < lowest)
		lowest = testScore4;
	else if (testScore5 < lowest)
		lowest = testScore5;

	return lowest;
}




error code C4700: Uninitialized local variable "testScore1" used.
error code C4700: Uninitialized local variable "testScore2" used.
error code C4700: Uninitialized local variable "testScore3" used.
error code C4700: Uninitialized local variable "testScore4" used.
error code C4700: Uninitialized local variable "testScore5" used.
error code C4700: Uninitialized local variable "avgMinusLowest" used.

So I set my main int variables all to 0, and then I can run the program, but the couts for average and lowest come back as 0. (I tested to make sure it was that and not my math by setting all values to 3. My returns were 3 & 2) When I try to remove avgMinusLowest from the calcAverage function arguments, it gives the error that my function doesn't have enough arguments. I've also tried this, but it gave me errors:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

int main()
{
	int testScore1, testScore2, testScore3, testScore4, testScore5, lowest, average, avgMinusLowest;

	testScore1 = getScore(testScore1);
	testScore2 = getScore(testScore2);
	testScore3 = getScore(testScore3);
	testScore4 = getScore(testScore4);
	testScore5 = getScore(testScore5);

	lowest = findLowest(testScore1, testScore2, testScore3, testScore4, testScore5);
	cout << "The lowest score is: " << lowest << endl;

	average = calcAverage(testScore1, testScore2, testScore3, testScore4, testScore5, lowest, avgMinusLowest);
	cout << "The average is " << average << endl;
	
	return 0;
}


I've seen this assignment 50 times over on this site and others with problems in the code but not this problem, with nearly exactly the same coding (difference is only the variable names) and no one mentions having these error codes. What am I doing wrong???? Please help!
Last edited on
First, getScore doesn't return a value, so lines 6 through 10 are trying to assign non-existent values to variables, which is an error.

Second, the testScore variables are not assigned any values before they are fed to a function that takes them by value. The only reason a function would take arguments by value is if the function intended to use the values contained in the parameters within the function, thus the errors. I suspect you meant to have getScore take an argument by reference, which would avoid that particular pitfall (and would make sense with the body of the function being what it is.)
The problem is that you're passing a variable to a function, then assigning that variable a value. When you're passing a variable to a function, you're passing its held value, not a reference. Every time you try to modify testScore1,2,3,4,5 in getScore, you're not actually modifying it, but a copy. Once the function ends, the value disappears and testScore is how you left it before calling getScore.

You're close, thugh. Your getScore function currently returns void, but you assign testScore1,2,3,4,5 to the value returned by getScore. You also pass testScore to getScore, but as I said earlier, this won't do anything. Try re-writing getScore so that you don't have to pass anything to it, but it returns the value that the user writes.
So void is my problem? I'll try changing that to int and write a return. It will likely take me a while to play with it, but I wanted to reply back quickly to say thank you. I will reply again after trying. Thanks again
Ok I tried changing getScore function from void to int, and made changes to lines 12- 16, but I'm still getting the same error codes. I'm sorry if I'm missing the point you guys are making. I really don't get what I'm doing wrong.

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>
using namespace std;

int getScore(int userInput);
int calcAverage(int testScore1, int testScore2, int testScore3, int testScore4, int testScore5, int lowest, int avgMinusLowest);
int findLowest(int testScore1, int testScore2, int testScore3, int testScore4, int testScore5);

int main()
{
	int testScore1, testScore2, testScore3, testScore4, testScore5, lowest, average, avgMinusLowest;

	testScore1 = getScore(testScore1);
	testScore2 = getScore(testScore2);
	testScore3 = getScore(testScore3);
	testScore4 = getScore(testScore4);
	testScore5 = getScore(testScore5);

	lowest = findLowest(testScore1, testScore2, testScore3, testScore4, testScore5);
	cout << "The lowest score is: " << lowest << endl;

	average = calcAverage(testScore1, testScore2, testScore3, testScore4, testScore5, lowest, avgMinusLowest);
	cout << "The average is " << average << endl;
	
	return 0;
}

int getScore(int userInput)
{
	cout << "Enter a test score: ";
	cin >> userInput;

	while (userInput < 1 || userInput > 100)
	{
		cout << "Error! You must enter a value from 0-100. Try again: ";
		cin >> userInput;

	}

	return userInput;
}

int calcAverage(int testScore1, int testScore2, int testScore3, int testScore4, int testScore5, int lowest, int avgMinusLowest)
{

	lowest = findLowest(testScore1, testScore2, testScore3, testScore4, testScore5);
	avgMinusLowest = ((testScore1 + testScore2 + testScore3 + testScore4 + testScore5 - lowest) / 5);

	return avgMinusLowest;
}

int findLowest(int testScore1, int testScore2, int testScore3, int testScore4, int testScore5)
{
	int lowest;
	lowest = testScore1;

	if (testScore2 < lowest)
		lowest = testScore2;
	else if (testScore3 < lowest)
		lowest = testScore3;
	else if (testScore4 < lowest)
		lowest = testScore4;
	else if (testScore5 < lowest)
		lowest = testScore5;

	return lowest;
}


Never mind, I got it!!! Thank you so much for your help!!!!

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>
using namespace std;

int getScore(int userInput);
int calcAverage(int testScore1, int testScore2, int testScore3, int testScore4, int testScore5, int lowest, int avgMinusLowest);
int findLowest(int testScore1, int testScore2, int testScore3, int testScore4, int testScore5);

int main()
{
	int testScore1 = 0, testScore2 = 0, testScore3 = 0, testScore4 = 0, testScore5 = 0, lowest, average, avgMinusLowest = 0;

	testScore1 = getScore(testScore1);
	testScore2 = getScore(testScore2);
	testScore3 = getScore(testScore3);
	testScore4 = getScore(testScore4);
	testScore5 = getScore(testScore5);

	lowest = findLowest(testScore1, testScore2, testScore3, testScore4, testScore5);
	cout << "The lowest score is: " << lowest << endl;

	average = calcAverage(testScore1, testScore2, testScore3, testScore4, testScore5, lowest, avgMinusLowest);
	cout << "The average is " << average << endl;
	
	return 0;
}

int getScore(int userInput)
{
	cout << "Enter a test score: ";
	cin >> userInput;

	while (userInput < 1 || userInput > 100)
	{
		cout << "Error! You must enter a value from 0-100. Try again: ";
		cin >> userInput;

	}

	return userInput;
}

int calcAverage(int testScore1, int testScore2, int testScore3, int testScore4, int testScore5, int lowest, int avgMinusLowest)
{

	lowest = findLowest(testScore1, testScore2, testScore3, testScore4, testScore5);
	avgMinusLowest = ((testScore1 + testScore2 + testScore3 + testScore4 + testScore5 - lowest) / 5);

	return avgMinusLowest;
}

int findLowest(int testScore1, int testScore2, int testScore3, int testScore4, int testScore5)
{
	int lowest;
	lowest = testScore1;

	if (testScore2 < lowest)
		lowest = testScore2;
	else if (testScore3 < lowest)
		lowest = testScore3;
	else if (testScore4 < lowest)
		lowest = testScore4;
	else if (testScore5 < lowest)
		lowest = testScore5;

	return lowest;
}

It works, but technically you don't even need, on line 27, int userInput. Just declare userInput in the function itself. Don't pass testScore to getScore (so remove the things in the parenthesis on lines 6-10). It'll work all the same, but will avoid passing something unnecessarily.
Topic archived. No new replies allowed.