Compile Your Code
If you compile your code, the compiler tells you what are the errors. You should at least make your code compile before you start asking questions.
Your array Weatherdata_array is not declared.
Even if it would be declared (define), you read Weatherdata_array, but you never write it. Hence the read value would be memory garbage.
You read it here:
high_temp = Weatherdata_array[0];
but the content of Weatherdata_array is uninitialized, undetermined, some random value from memory. Fill it with values before you try to read it.
If you want to initialise an array to "empty" you can do it like this:
Weatherdata Weatherdata_array[12] = { 0 };
A Better Solution
I have rewritten your code so it now works. Also, I'd like to point out that the average temperature probably has nothing to do with the the average of the minimum and maximum temperatures (min + max / 2). You can have 1 day that's cold, and 29 days that's hot in the month. The average will be close to hot, not in between the highest and lowers amount.
The whole project can be downloaded from here: https://onlinegdb.com/HyluRHjqD
Months.h
#pragma once
#include <string>
const int nMonths = 12; // Number of Months
const std::string monthNames[nMonths] = { "January", "Februray", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
WeatherData.h
#pragma once
struct WeatherData
{
double total_rainfall;
double high_temp;
double low_temp;
double avg_temp;
WeatherData();
virtual void Print() const;
};
WeatherData.cpp
#include "WeatherData.h"
#include <iostream>
WeatherData::WeatherData() :
total_rainfall(0.0),
high_temp(0.0),
low_temp(0.0),
avg_temp(0.0)
{
}
void WeatherData::Print() const
{
std::cout << "Total rainfall: " << total_rainfall << " mm" << std::endl;
std::cout << "Highest temperature: " << high_temp << " C" << std::endl;
std::cout << "Lowest temperature: " << low_temp << " C" << std::endl;
std::cout << "Average temperature: " << avg_temp << " C" << std::endl;
}
WeatherDataSummary.h
#pragma once
#include "WeatherData.h"
struct WeatherDataSummary : public WeatherData
{
int high_temp_month;
int low_temp_month;
WeatherDataSummary();
WeatherDataSummary(const WeatherData & weatherData);
void Print() const override;
};
WeatherDataSummary.cpp
#include "WeatherDataSummary.h"
#include "Months.h"
#include <iostream>
WeatherDataSummary::WeatherDataSummary() :
WeatherData(),
high_temp_month(0),
low_temp_month(0)
{
}
WeatherDataSummary::WeatherDataSummary(const WeatherData & weatherData)
{
total_rainfall = weatherData.total_rainfall;
high_temp = weatherData.high_temp;
low_temp = weatherData.low_temp;
avg_temp = weatherData.avg_temp;
}
void WeatherDataSummary::Print() const
{
std::cout << "Total rainfall: " << total_rainfall << " mm" << std::endl;
std::cout << "Highest temperature: " << high_temp << " C [" << monthNames[high_temp_month] << "]" << std::endl;
std::cout << "Lowest temperature: " << low_temp << " C [" << monthNames[low_temp_month] << "]" << std::endl;
std::cout << "Average temperature: " << avg_temp << " C" << std::endl;
}
WeatherDataStatistics.h
#pragma once
#include "Months.h"
#include "WeatherDataSummary.h"
#include <string>
class WeatherDataStatistics
{
public:
WeatherDataStatistics();
void ReadDataFromUser();
void Calculate();
void Print() const;
private:
WeatherData monthlyData[nMonths];
WeatherData monthlyAverage;
WeatherDataSummary yearlySummary;
double ReadValidated(const std::string & message, double min, double max) const;
void Print(const WeatherData & data) const;
};
WeatherDataStatistics.cpp
#include "WeatherDataStatistics.h"
#include <iostream>
WeatherDataStatistics::WeatherDataStatistics() :
monthlyData(),
monthlyAverage(),
yearlySummary()
{
}
double WeatherDataStatistics::ReadValidated(const std::string & message, double min, double max) const
{
double value;
do
{
std::cout << message << " ([" << min << ", " << max << "]): ";
std::cin >> value;
} while (value < min || value > max);
return value;
}
void WeatherDataStatistics::ReadDataFromUser()
{
for (int i = 0; i < nMonths; i++)
{
std::cout << "Enter the data for " << monthNames[i] << "." << std::endl;
monthlyData[i].total_rainfall = ReadValidated("Enter the total rainfall", 0, 100000); // TODO: you can come up with a reasonable number here. Google search puts the highest average rainfall around 12000 m
monthlyData[i].high_temp = ReadValidated("Enter the highest temperature", -140, 140);
monthlyData[i].low_temp = ReadValidated("Enter the lowest temperature", -140, 140);
monthlyData[i].avg_temp = ReadValidated("Enter the average temperature", -140, 140);
std::cout << std::endl;
}
}
void WeatherDataStatistics::Calculate()
{
monthlyAverage = monthlyData[0];
yearlySummary = monthlyData[0];
for (int i = 1; i < nMonths; i++) // we've already "processed" the 1st month
{
yearlySummary.total_rainfall += monthlyData[i].total_rainfall;
if (yearlySummary.high_temp < monthlyData[i].high_temp)
{
yearlySummary.high_temp = monthlyData[i].high_temp;
yearlySummary.high_temp_month = i;
}
if (yearlySummary.low_temp > monthlyData[i].low_temp)
{
yearlySummary.low_temp = monthlyData[i].low_temp;
yearlySummary.low_temp_month = i;
}
yearlySummary.avg_temp += monthlyData[i].avg_temp;
monthlyAverage.total_rainfall += monthlyData[i].total_rainfall;
monthlyAverage.high_temp += monthlyData[i].high_temp;
monthlyAverage.low_temp += monthlyData[i].low_temp;
monthlyAverage.avg_temp += monthlyData[i].avg_temp;
}
yearlySummary.avg_temp /= 12;
monthlyAverage.total_rainfall /= 12;
monthlyAverage.high_temp /= 12;
monthlyAverage.low_temp /= 12;
monthlyAverage.avg_temp /= 12;
}
void WeatherDataStatistics::Print() const
{
std::cout << "Montly average (for a full year):" << std::endl;
Print(monthlyAverage);
std::cout << std::endl << "Yearly summary:" << std::endl;
Print(yearlySummary);
}
void WeatherDataStatistics::Print(const WeatherData & data) const
{
std::cout << "Total rainfall: " << data.total_rainfall << " mm" << std::endl;
std::cout << "Highest temperature: " << data.high_temp << " C" << std::endl;
std::cout << "Lowest temperature: " << data.low_temp << " C" << std::endl;
std::cout << "Average temperature: " << data.avg_temp << " C" << std::endl;
}
main.cpp
#include "WeatherDataStatistics.h"
int main()
{
WeatherDataStatistics weatherDataStatistics;
weatherDataStatistics.ReadDataFromUser();
weatherDataStatistics.Calculate();
weatherDataStatistics.Print();
return 0;
}