fork download
  1. //Charlotte Davies-Kiernan CS1A Chapter 11 P. 646 #5
  2. //
  3. /******************************************************************************
  4.  *
  5.  * Compute Rainfall Measurements (Modified)
  6.  * ____________________________________________________________________________
  7.  * This program will accept data from the user about the rainfall and
  8.  * temperature (highest and lowest) each month then calculate the total
  9.  * rainfall, average monthly rainfall, which temperature was the hgiehst/lowest
  10.  * and what month it was during, and lastly the yearly average temperature!
  11.  * ____________________________________________________________________________
  12.  * Input
  13.  * totalRainfall :Total amount of rainfall in the month
  14.  * highTemp :Highest temperature in the month
  15.  * lowTemp :Lowest temperature in the month
  16.  * avgTemp :Average temperature between lowest and high in the month
  17.  * Output
  18.  * totalRainfall :Total amount of rainfall in the year
  19.  * averageMonthlyRainfall :Average amount of rainfall in the year
  20.  * totalAverageTemp :Average temperature in the year
  21.  * highestMonth :Highest temperature and the month it came from
  22.  * lowestMonth :Lowest temperature and the month it came from
  23.  *****************************************************************************/
  24. #include <iostream>
  25. #include <iomanip>
  26. #include <string>
  27. using namespace std;
  28.  
  29. //Enumerated type for months
  30. enum Month {
  31. JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE,
  32. JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER
  33. };
  34.  
  35. struct Weather {
  36. float totalRainfall;
  37. float highTemp;
  38. float lowTemp;
  39. float avgTemp;
  40. };
  41.  
  42. //Function Prototypes
  43. float getValidTemperature(const string &prompt);
  44. float getPositiveFloat(const string &prompt);
  45. string getMonthName(Month m);
  46.  
  47. int main() {
  48. //Data Dictionary
  49. const int NUM_MONTHS = 12;
  50. Weather year[NUM_MONTHS];
  51. float totalRainfallYear = 0;
  52. float totalAverageTemp = 0;
  53. float highestTemp = -101;
  54. Month highestMonth = JANUARY;
  55. float lowestTemp = 141;
  56. Month lowestMonth = JANUARY;
  57. float averageMonthlyRainfall;
  58. float yearlyAverageTemp;
  59.  
  60. //Input Data for Each Month
  61. for(Month m = JANUARY; m < DECEMBER; m = static_cast<Month>(m + 1)) {
  62. cout << "Enter data for " << getMonthName(m) << endl;
  63.  
  64. year[m].totalRainfall = getPositiveFloat("Total Rainfall: ");
  65. year[m].highTemp = getValidTemperature("High Temperature: ");
  66. year[m].lowTemp = getValidTemperature("Low Temperature: ");
  67. //Calculate Average for Month
  68. year[m].avgTemp = (year[m].highTemp + year[m].lowTemp) / 2.0;
  69. //Add to Yearly Totals
  70. totalRainfallYear += year[m].totalRainfall;
  71. totalAverageTemp += year[m].avgTemp;
  72. //Check Highest and Lowest Temperature
  73. if(year[m].highTemp > highestTemp) {
  74. highestTemp = year[m].highTemp;
  75. highestMonth = m;
  76. }
  77. if(year[m].lowTemp < lowestTemp) {
  78. lowestTemp = year[m].lowTemp;
  79. lowestMonth = m;
  80. }
  81. }
  82. //Calculate Average Monthly Rainfall
  83. averageMonthlyRainfall = totalRainfallYear / NUM_MONTHS;
  84. //Calculate Average of Monthly Averages
  85. yearlyAverageTemp = totalAverageTemp / NUM_MONTHS;
  86.  
  87. //Output Results
  88. cout << "\n\nWEATHER REPORT!: " << endl;
  89. cout << "Total Rainfall for the Year: " << totalRainfallYear << " inches." << endl;
  90. cout << "Average Monthly Rainfall: " << averageMonthlyRainfall << " inches." << endl;
  91. cout << "Highest Temperature: " << highestTemp << " F in " << getMonthName(highestMonth) << endl;
  92. cout << "Lowest Temperature: " << lowestTemp << " F in " << getMonthName(lowestMonth) << endl;
  93. cout << "Yearly Average Temperature: " << yearlyAverageTemp << " F" << endl;
  94.  
  95. return 0;
  96. }
  97.  
  98. //Function Definitions!
  99. float getPositiveFloat(const string &prompt) {
  100. float value;
  101. while(true) {
  102. cout << prompt;
  103. cin >> value;
  104.  
  105. if(!cin.fail() && value >= 0)
  106. return value;
  107.  
  108. cin.clear();
  109. cin.ignore(1000, '\n');
  110. cout << "Invalid Input. Please enter a non-negative number." << endl;
  111. }
  112. }
  113. float getValidTemperature(const string &prompt){
  114. float temp;
  115. while(true) {
  116. cout << prompt;
  117. cin >> temp;
  118.  
  119. if(!cin.fail() && temp >= -100 && temp <= 140)
  120. return temp;
  121.  
  122. cin.clear();
  123. cin.ignore(1000, '\n');
  124. cout << "Invalid input. Temperature must be between -100 and 140!" << endl;
  125. }
  126. }
  127. string getMonthName(Month m) {
  128. const string names[12] = {
  129. "January", "February", "March", "April", "May", "June",
  130. "July", "August", "September", "October", "november", "December"
  131. };
  132.  
  133. return names[m];
  134. }
Success #stdin #stdout 0.01s 5328KB
stdin
3.2 
40
20
2.5
45
15
3.8
55
30
4.2
60
35
5.0
72
50
4.7
85
60
3.1
95
70
3.3
100
68
4.0
88
55
3.6
70
45
2.9
55
32
3.4
48
25
stdout
Enter data for January
Total Rainfall: High Temperature: Low Temperature: Enter data for February
Total Rainfall: High Temperature: Low Temperature: Enter data for March
Total Rainfall: High Temperature: Low Temperature: Enter data for April
Total Rainfall: High Temperature: Low Temperature: Enter data for May
Total Rainfall: High Temperature: Low Temperature: Enter data for June
Total Rainfall: High Temperature: Low Temperature: Enter data for July
Total Rainfall: High Temperature: Low Temperature: Enter data for August
Total Rainfall: High Temperature: Low Temperature: Enter data for September
Total Rainfall: High Temperature: Low Temperature: Enter data for October
Total Rainfall: High Temperature: Low Temperature: Enter data for november
Total Rainfall: High Temperature: Low Temperature: 

WEATHER REPORT!: 
Total Rainfall for the Year: 40.3 inches.
Average Monthly Rainfall: 3.35833 inches.
Highest Temperature: 100 F in August
Lowest Temperature: 15 F in February
Yearly Average Temperature: 51.875 F