Function variables not being initialized

Hi. I am trying to create a program that opens a pre-written text file and reads the numbers from the list which is then computed in order to obtain the min, max, and average of the file. Finally, it will then display this information on the console screen. However, every time I attempt to run the code, my program tells me that my function variables aren't being initialized. Anyone care to look over my coding and tell me 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
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;
void calculations (double &min, double &max, double &average);
void results (double min, double max, double average);

int main()
{
	double min, max, average;
	
	
	calculations (min, max, average);
	results (min, max, average);
	
	std::cin.ignore();
	std::cin.get();

	return 0;
}

void calculations (double &min, double &max, double &average)
{
	int counter=0;
	double number,
		   sum=0,
		   average,
    maximum = -99999,
    minimum = 99999;
	ifstream inFile;
	inFile.open("numbers.txt");
	if(inFile.fail())
	{
		cout << "File could not be opened.";
		exit(1);
	}

	while (inFile>>number)
	{
		if (number>max)
		{
		     max=number;
		}
		if (number<min)
		{
			min=number;
		}

        sum=sum+number;
        counter++;
	}

    average=sum/counter;

	inFile.close();

}
void results (double min, double max, double average)
{
	cout << "The minimum is: " << setprecision(2) << min << endl;
	cout << "The maximum is: " << setprecision(2) << max << endl;
	cout <<	"The average is: " << setprecision(2) << average << endl;
}
The error message tells you exactly what the problem is. Your variables aren't being initialized.

1
2
3
4
5
int main()
{
	double min, max, average; //<--change this

        double min = 0, max = 0, average = 0; // <--to this. 
Thumper,

Thank you for the quick reply. I utilized your suggestion and am still receiving the error. Any other ideas as to what may be the problem?
1
2
3
4
5
6
7
8
void calculations (double &min, double &max, double &average) 
{                                                                         
    int counter=0;         
        double number, //<-- needs to be initialized to something   
        sum=0,
        average, //<-- Delete this or use a different name for it.
        //You're re-declaring it because you already used the name 'average'
        //as a function parameter. 
Thank you once again Thumper.

That did solve my problem of displaying to the console. However, I was unsure as to what I should initialize "double number" to be so I set it = 0, resulting in my min and average to display 0. And suggestions as to what I should use to initialize it?
Set min to equal the very first number in the set to compare, or you can set it to a value that exceeds the max possible input value from the value, which is basically what you did with max. Since max was = 0, even the first value you read in is going to be greater then it so that first read will set max = number. Unfortunately, with min, 0 will be less then any possible number you input (assuming they are all positive of course) so your min will always be 0. Usually the way you find min and max, at least from an array, is something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
int size = 20;
int array[size] = { whatever 20 numbers you want or input };
int min, max;
min = array[0];
max = array[0];

for (int i = 1; i < size; i++)
{
    if (min > array[i])
        min = array[i];
    else if (max < array[i])            
        max = array[i];
}


With min and max initialized to the first element of the data you are guaranteed that if the first element IS the min, or the max, that it'll still be accounted for. And you set i = 1 because you already have the 1st element of the array loaded into the variables so you don't need to check min and max against array[0].

Edit: BAH I typed all that up and now noticed you said you set NUMBER to 0. *sigh* It shouldn't have matter either way since you input to number before you use it for anything. Hmmm... Oh, one error I do see is that you are declaring average again in your function which is hiding the one you declared in main then passed by reference to the calculations function. Try deleting line 28 and see what that does for ya.

Edit 2: Man I need some sleep, I see that thumper said exactly that too. I give up heh.
Last edited on
Topic archived. No new replies allowed.