//Charlotte Davies-Kiernan CS1A Chapter 11 P. 646 #5
//
/******************************************************************************
*
* Compute Rainfall Measurements (Modified)
* ____________________________________________________________________________
* This program will accept data from the user about the rainfall and
* temperature (highest and lowest) each month then calculate the total
* rainfall, average monthly rainfall, which temperature was the hgiehst/lowest
* and what month it was during, and lastly the yearly average temperature!
* ____________________________________________________________________________
* Input
* totalRainfall :Total amount of rainfall in the month
* highTemp :Highest temperature in the month
* lowTemp :Lowest temperature in the month
* avgTemp :Average temperature between lowest and high in the month
* Output
* totalRainfall :Total amount of rainfall in the year
* averageMonthlyRainfall :Average amount of rainfall in the year
* totalAverageTemp :Average temperature in the year
* highestMonth :Highest temperature and the month it came from
* lowestMonth :Lowest temperature and the month it came from
*****************************************************************************/
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
//Enumerated type for months
enum Month {
JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE,
JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER
};
struct Weather {
float totalRainfall;
float highTemp;
float lowTemp;
float avgTemp;
};
//Function Prototypes
float getValidTemperature(const string &prompt);
float getPositiveFloat(const string &prompt);
string getMonthName(Month m);
int main() {
//Data Dictionary
const int NUM_MONTHS = 12;
Weather year[NUM_MONTHS];
float totalRainfallYear = 0;
float totalAverageTemp = 0;
float highestTemp = -101;
Month highestMonth = JANUARY;
float lowestTemp = 141;
Month lowestMonth = JANUARY;
float averageMonthlyRainfall;
float yearlyAverageTemp;
//Input Data for Each Month
for(Month m = JANUARY; m < DECEMBER; m = static_cast<Month>(m + 1)) {
cout << "Enter data for " << getMonthName(m) << endl;
year[m].totalRainfall = getPositiveFloat("Total Rainfall: ");
year[m].highTemp = getValidTemperature("High Temperature: ");
year[m].lowTemp = getValidTemperature("Low Temperature: ");
//Calculate Average for Month
year[m].avgTemp = (year[m].highTemp + year[m].lowTemp) / 2.0;
//Add to Yearly Totals
totalRainfallYear += year[m].totalRainfall;
totalAverageTemp += year[m].avgTemp;
//Check Highest and Lowest Temperature
if(year[m].highTemp > highestTemp) {
highestTemp = year[m].highTemp;
highestMonth = m;
}
if(year[m].lowTemp < lowestTemp) {
lowestTemp = year[m].lowTemp;
lowestMonth = m;
}
}
//Calculate Average Monthly Rainfall
averageMonthlyRainfall = totalRainfallYear / NUM_MONTHS;
//Calculate Average of Monthly Averages
yearlyAverageTemp = totalAverageTemp / NUM_MONTHS;
//Output Results
cout << "\n\nWEATHER REPORT!: " << endl;
cout << "Total Rainfall for the Year: " << totalRainfallYear << " inches." << endl;
cout << "Average Monthly Rainfall: " << averageMonthlyRainfall << " inches." << endl;
cout << "Highest Temperature: " << highestTemp << " F in " << getMonthName(highestMonth) << endl;
cout << "Lowest Temperature: " << lowestTemp << " F in " << getMonthName(lowestMonth) << endl;
cout << "Yearly Average Temperature: " << yearlyAverageTemp << " F" << endl;
return 0;
}
//Function Definitions!
float getPositiveFloat(const string &prompt) {
float value;
while(true) {
cout << prompt;
cin >> value;
if(!cin.fail() && value >= 0)
return value;
cin.clear();
cin.ignore(1000, '\n');
cout << "Invalid Input. Please enter a non-negative number." << endl;
}
}
float getValidTemperature(const string &prompt){
float temp;
while(true) {
cout << prompt;
cin >> temp;
if(!cin.fail() && temp >= -100 && temp <= 140)
return temp;
cin.clear();
cin.ignore(1000, '\n');
cout << "Invalid input. Temperature must be between -100 and 140!" << endl;
}
}
string getMonthName(Month m) {
const string names[12] = {
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "november", "December"
};
return names[m];
}