//Charlotte Davies-Kiernan CS1A Chapter 11 P. 646 #4
//
/******************************************************************************
*
* Compute Rainfall Measurements
* ____________________________________________________________________________
* 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;
struct Weather {
float totalRainfall;
float highTemp;
float lowTemp;
float avgTemp;
};
//Function Prototypes
float getValidTemperature(const string &prompt);
float getPositiveFloat(const string &prompt);
string getMonthName(int index);
int main() {
//Data Dictionary
Weather year[12];
const int NUM_MONTHS = 12;
float totalRainfallYear = 0;
float averageMonthlyRainfall = 0;
float totalAverageTemp = 0;
float highestTemp = -101;
int highestMonth = 0;
float lowestTemp = 141;
int lowestMonth = 0;
float yearlyAverageTemp;
//Input Data for Each Month
for(int i = 0; i < NUM_MONTHS; i++) {
cout << "Enter data for " << getMonthName(i) << endl;
year[i].totalRainfall = getPositiveFloat("Total Rainfall: ");
year[i].highTemp = getValidTemperature("High Temperature: ");
year[i].lowTemp = getValidTemperature("Low Temperature: ");
//Calculate Average for Month
year[i].avgTemp = (year[i].highTemp + year[i].lowTemp) / 2.0;
//Add to Yearly Totals
totalRainfallYear += year[i].totalRainfall;
totalAverageTemp += year[i].avgTemp;
//Check Highest and Lowest Temperature
if(year[i].highTemp > highestTemp) {
highestTemp = year[i].highTemp;
highestMonth = i;
}
if(year[i].lowTemp < lowestTemp) {
lowestTemp = year[i].lowTemp;
lowestMonth = i;
}
}
//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(int index) {
const string months[12] = {
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "november", "December"
};
return months[index];
}