// Torrez, Elaine CS1A
// ----------------------------------------------
// WEATHER STATISTICS MODIFICATION
// ----------------------------------------------
//
// This program is a modification of the Weather Statistics
// program. It stores weather data for 12 months using a
// structure and an enumerated data type for the months.
// For each month, the user enters the total rainfall, the
// high temperature, and the low temperature. The program
// calculates the average monthly rainfall, total yearly
// rainfall, highest temperature and the month it occurred,
// lowest temperature and the month it occurred, and the
// average temperature for the entire year.
//
// This version defines an enumerated type with enumerators
// for the months and uses it to step through the array.
//
// ----------------------------------------------
// INPUT
// For each of the 12 months:
// total rainfall
// high temperature
// low temperature
//
// PROCESSING
// Use enum Month {JANUARY, ..., DECEMBER}
// Validate input (rain >= 0, temp between -100 and 140)
// Compute total rainfall
// Compute average monthly rainfall
// Identify highest and lowest temp + month
// Compute average temperature for the year
//
// OUTPUT
// Total rainfall for the year
// Average rainfall
// Highest temperature and month
// Lowest temperature and month
// Average yearly temperature
//
// ----------------------------------------------
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
// ----------------------------------------------
// Enumerated type for the months
// ----------------------------------------------
enum Month { JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE,
JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER };
// ----------------------------------------------
// Structure to store weather data
// ----------------------------------------------
struct Weather {
double rainfall; // Total rainfall for the month
double highTemp; // Highest temperature
double lowTemp; // Lowest temperature
double avgTemp; // Average monthly temperature
};
int main()
{
const int MONTHS = 12;
string monthNames[MONTHS] =
{ "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" };
Weather data[MONTHS];
double totalRain = 0;
double totalAvgTemp = 0;
double highest = -101;
double lowest = 141;
Month highMonth = JANUARY;
Month lowMonth = JANUARY;
cout << fixed << setprecision(2);
// ----------------------------------------------------
// INPUT SECTION (step through array using enum)
// ----------------------------------------------------
for (Month m = JANUARY; m <= DECEMBER; m = static_cast<Month>(m + 1))
{
int i = static_cast<int>(m); // use enum as index
cout << "\nEnter data for " << monthNames[i] << ":\n";
// Rainfall validation
do {
cout << " Total rainfall: ";
cin >> data[i].rainfall;
if (data[i].rainfall < 0)
cout << " Error: Rainfall cannot be negative.\n";
} while (data[i].rainfall < 0);
// High temperature validation
do {
cout << " High temperature: ";
cin >> data[i].highTemp;
if (data[i].highTemp < -100 || data[i].highTemp > 140)
cout << " Error: Temp must be between -100 and 140.\n";
} while (data[i].highTemp < -100 || data[i].highTemp > 140);
// Low temperature validation
do {
cout << " Low temperature: ";
cin >> data[i].lowTemp;
if (data[i].lowTemp < -100 || data[i].lowTemp > 140)
cout << " Error: Temp must be between -100 and 140.\n";
} while (data[i].lowTemp < -100 || data[i].lowTemp > 140);
// Average temperature for this month
data[i].avgTemp = (data[i].highTemp + data[i].lowTemp) / 2.0;
// Add to totals
totalRain += data[i].rainfall;
totalAvgTemp += data[i].avgTemp;
// Track highest temp
if (data[i].highTemp > highest) {
highest = data[i].highTemp;
highMonth = m;
}
// Track lowest temp
if (data[i].lowTemp < lowest) {
lowest = data[i].lowTemp;
lowMonth = m;
}
}
// ----------------------------------------------------
// OUTPUT SECTION
// ----------------------------------------------------
cout << "\n----------------------------------------------\n";
cout << "WEATHER STATISTICS REPORT\n";
cout << "----------------------------------------------\n";
cout << "Total Rainfall: " << totalRain << " inches\n";
cout << "Average Monthly Rainfall: " << totalRain / MONTHS << " inches\n";
cout << "Highest Temperature: " << highest
<< " ( " << monthNames[static_cast<int>(highMonth)] << " )\n";
cout << "Lowest Temperature: " << lowest
<< " ( " << monthNames[static_cast<int>(lowMonth)] << " )\n";
cout << "Average Temperature for the Year: "
<< totalAvgTemp / MONTHS << " degrees\n";
return 0;
}