fork download
  1. //Charlotte Davies-Kiernan CS1A Chapter 11 P. 646 #4
  2. //
  3. /******************************************************************************
  4.  *
  5.  * Compute Rainfall Measurements
  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. struct Weather {
  30. float totalRainfall;
  31. float highTemp;
  32. float lowTemp;
  33. float avgTemp;
  34. };
  35.  
  36. //Function Prototypes
  37. float getValidTemperature(const string &prompt);
  38. float getPositiveFloat(const string &prompt);
  39. string getMonthName(int index);
  40.  
  41. int main() {
  42. //Data Dictionary
  43. Weather year[12];
  44. const int NUM_MONTHS = 12;
  45. float totalRainfallYear = 0;
  46. float averageMonthlyRainfall = 0;
  47. float totalAverageTemp = 0;
  48. float highestTemp = -101;
  49. int highestMonth = 0;
  50. float lowestTemp = 141;
  51. int lowestMonth = 0;
  52. float yearlyAverageTemp;
  53.  
  54. //Input Data for Each Month
  55. for(int i = 0; i < NUM_MONTHS; i++) {
  56. cout << "Enter data for " << getMonthName(i) << endl;
  57.  
  58. year[i].totalRainfall = getPositiveFloat("Total Rainfall: ");
  59. year[i].highTemp = getValidTemperature("High Temperature: ");
  60. year[i].lowTemp = getValidTemperature("Low Temperature: ");
  61. //Calculate Average for Month
  62. year[i].avgTemp = (year[i].highTemp + year[i].lowTemp) / 2.0;
  63. //Add to Yearly Totals
  64. totalRainfallYear += year[i].totalRainfall;
  65. totalAverageTemp += year[i].avgTemp;
  66. //Check Highest and Lowest Temperature
  67. if(year[i].highTemp > highestTemp) {
  68. highestTemp = year[i].highTemp;
  69. highestMonth = i;
  70. }
  71. if(year[i].lowTemp < lowestTemp) {
  72. lowestTemp = year[i].lowTemp;
  73. lowestMonth = i;
  74. }
  75. }
  76. //Calculate Average Monthly Rainfall
  77. averageMonthlyRainfall = totalRainfallYear / NUM_MONTHS;
  78. //Calculate Average of Monthly Averages
  79. yearlyAverageTemp = totalAverageTemp / NUM_MONTHS;
  80.  
  81. //Output Results
  82. cout << "\n\nWEATHER REPORT!: " << endl;
  83. cout << "Total Rainfall for the Year: " << totalRainfallYear << " inches." << endl;
  84. cout << "Average Monthly Rainfall: " << averageMonthlyRainfall << " inches." << endl;
  85. cout << "Highest Temperature: " << highestTemp << " F in " << getMonthName(highestMonth) << endl;
  86. cout << "Lowest Temperature: " << lowestTemp << " F in " << getMonthName(lowestMonth) << endl;
  87. cout << "Yearly Average Temperature: " << yearlyAverageTemp << " F" << endl;
  88.  
  89. return 0;
  90. }
  91.  
  92. //Function Definitions!
  93. float getPositiveFloat(const string &prompt) {
  94. float value;
  95. while(true) {
  96. cout << prompt;
  97. cin >> value;
  98.  
  99. if(!cin.fail() && value >= 0)
  100. return value;
  101.  
  102. cin.clear();
  103. cin.ignore(1000, '\n');
  104. cout << "Invalid Input. Please enter a non-negative number." << endl;
  105. }
  106. }
  107. float getValidTemperature(const string &prompt){
  108. float temp;
  109. while(true) {
  110. cout << prompt;
  111. cin >> temp;
  112.  
  113. if(!cin.fail() && temp >= -100 && temp <= 140)
  114. return temp;
  115.  
  116. cin.clear();
  117. cin.ignore(1000, '\n');
  118. cout << "Invalid input. Temperature must be between -100 and 140!" << endl;
  119. }
  120. }
  121. string getMonthName(int index) {
  122. const string months[12] = {
  123. "January", "February", "March", "April", "May", "June",
  124. "July", "August", "September", "October", "november", "December"
  125. };
  126. return months[index];
  127. }
Success #stdin #stdout 0.01s 5276KB
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: Enter data for December
Total Rainfall: High Temperature: Low Temperature: 

WEATHER REPORT!: 
Total Rainfall for the Year: 43.7 inches.
Average Monthly Rainfall: 3.64167 inches.
Highest Temperature: 100 F in August
Lowest Temperature: 15 F in February
Yearly Average Temperature: 54.9167 F