#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 *);
constint 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!
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.