Why is this error stating my variable is unintialized?

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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <iostream>
#include <string>

struct WeatherStats
{
	double total_rainfall;
	int high_temp;
	int low_temp;
	int avg_temp;

	int avg_yearly_temp;
	int high_temp_month;
	int low_temp_month;
	double avg_monthly_rainfall;
};

bool acceptableTempRange(int);
WeatherStats getWeatherData(std::string);
WeatherStats getWeatherTotals(WeatherStats *);


const int MONTHS = 3;
int main()
{
	std::string month_names[MONTHS] = { "January", "February", "March" }; 
	
	WeatherStats monthly_weather_data[MONTHS];

	for (int count = 0; count < MONTHS; count++)
	{
		monthly_weather_data[count] = getWeatherData(month_names[count]);
	}

	WeatherStats weather_totals = getWeatherTotals(monthly_weather_data);

	

}


bool acceptableTempRange(int X)
{
	bool status = false;

	if (X < -100 && X > 140)
		std::cout << "This tempature is not in the acceptable range.\n";
	else
		status = true;
	return status;
}


WeatherStats getWeatherData(std::string month)
{
	WeatherStats temp;

	std::cout << "\t\t\tWeather Data for " << month << std::endl;

	std::cout << "Enter the total Rainfall: ";
	std::cin >> temp.total_rainfall;
	

	do{
		std::cout << "\nEnter the highest temperature in Farenheit: ";
		std::cin >> temp.high_temp;
	} while (!acceptableTempRange(temp.high_temp));

        do{
	        std::cout << "\nEnter the lowest temperature in Farenheit: ";
	        std::cin >> temp.low_temp;
        } while (!acceptableTempRange(temp.low_temp));

	temp.avg_temp = (temp.high_temp + temp.low_temp) / 2;
	std::cout << "\nAverage temperature for the month was " << temp.avg_temp << std::endl;

	
	return temp;
}

WeatherStats getWeatherTotals(WeatherStats * ptr)
{

	WeatherStats temp;

	temp.total_rainfall = 0.0;
	temp.high_temp = ptr[0].high_temp;
	temp.low_temp = ptr[0].low_temp;
	temp.avg_monthly_rainfall = 0;
	
	int total_yearly_temp = 0;

	for (int count = 0; count < MONTHS; count++)
	{
		temp.total_rainfall += ptr[count].total_rainfall;

		total_yearly_temp = total_yearly_temp + temp.avg_temp;

		if (ptr[count].high_temp > temp.high_temp)
		{
			temp.high_temp = ptr[count].high_temp;
			temp.high_temp_month = count;
		}

		if (ptr[count].low_temp < temp.low_temp)
		{
			temp.low_temp = ptr[count].low_temp;
			temp.low_temp_month = count;
		}
	}

	temp.avg_monthly_rainfall = temp.total_rainfall / MONTHS;
	temp.avg_yearly_temp = total_yearly_temp / MONTHS;
	return temp;
}

When I run this program I am able to input data for the three months but after inputting everything I am prompted with a run time error that states: Run time check failure#3: The variable 'temp' is being used without being initialized. I've underlined the statement that the compiler says is causing this error, yet there is no variable 'temp' in that statement. Computer are very specific right? So in the problem statement total_yearly_temp = total_yearly_temp + temp.avg_temp; there is a variable called total_yearly_temp and one called temp.avg_temp, but there are none called temp. It can't be complaining about the WeatherStats variable I defined in the first line of the function called temp because I did the exact same thing in the previous function and there are no errors concerning that. Please help me out if you can!
you're using
temp.avg_temp
and you haven't initialised the avg_temp part of it.

add this:
 
temp.avg_temp = 0;


to line 89.


but i would suggest changing your struct to a class and using an initialisation list in your class constructor.
but i would suggest changing your struct to a class and using an initialisation list in your class constructor.

Or even keeping it as a struct, and using an initialisation list in your struct constructor ;)

(There's a convention used by a lot of people that if you want the type to be used as a collection of data items, which are publicly accessible, then you indicate that by making it a struct, even if you have constructors/destructors/helper methods. This makes it clear at a glance that it's used in a different way from a class with a well-defined interface and hidden implementation.

Of course, this is a matter of style, and YMMV.)
Or even keeping it as a struct, and using an initialisation list in your struct constructor ;)

Good point :)
Topic archived. No new replies allowed.