1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <string>
struct weather
{
double totalRainfall;
double highTemp;
double lowTemp;
double avgTemp;
};
void waitForEnter();
int main()
{
// Uncomment the following lines to get data from the user
// weather months[12];
// std::string month[] = { "January", "February", "March", "April",
// "May", "June", "July", "August",
// "September", "October", "November", "December" };
// for (int i = 0; i<12; i++)
// {
// std::cout << "Enter total rainfall for month " << month[i] << ": ";
// std::cin >> months[i].totalRainfall;
// do {
// std::cout << "Enter highest temperature: ";
// std::cin >> months[i].highTemp;
// cin.ignore(1);
// if(months[i].highTemp < -100 || 140 < months[i].highTemp) {
// std::cout << "ERROR: temperature must be in the range of "
// "-100 through 140.\n";
// }
// } while (months[i].highTemp < -100 || 140 < months[i].highTemp)
// do {
// std::cout << "Enter low temperature: ";
// std::cin >> months[i].lowTemp;
// cin.ignore(1);
// if(months[i].lowTemp < -100 || 140 < months[i].lowTemp) {
// std::cout << "ERROR: temperature must be in the range of "
// "-100 through 40.\n";
// }
// } while (months[i].lowTemp < -100 || 140 < months[i].lowTemp)
// }
// Comment the following block to avoid reading data from test file
// START READING FROM FILE
std::ifstream test_data("rain_temp.txt");
weather months[12];
std::string month[] = { "January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December" };
for(int i{}; i<12; i++) {
test_data >> months[i].totalRainfall >> months[i].highTemp
>> months[i].lowTemp;
}
test_data.close();
// END READING FROM FILE
//Calculate the monthly average temperature and total rainfall.
double total = 0;
for (int i = 0; i<12; i++)
{
total += months[i].totalRainfall;
months[i].avgTemp = (months[i].highTemp + months[i].lowTemp)/2;
}
// Find the highest and lowest temperatures ever.
// Bookmark that month.
double highest = 0,
lowest = 141,
avgsum = 0;
int highmonth = 0, lowmonth = 0;
for (int i=0; i<12; i++)
{
if (months[i].highTemp > highest)
{
highest = months[i].highTemp;
highmonth = i;
}
if (months[i].lowTemp < lowest)
{
lowest = months[i].lowTemp;
lowmonth = i;
}
// Since later we want the average year temperature, we get ahead of
// that calculation.
avgsum += months[i].avgTemp;
}
// Calculate the average. <-- of what? Bad comment.
avgsum /= 12;
//Display the calculated data.
std::cout << "Total rainfall: " << total
<< "\nAverage monthly rainfall: " << total / 12
<< "\nHighest rainfall in " << month[highmonth] << " has been: " << highest
<< "\nLowest rainfall in " << month[lowmonth] << " has been: " << lowest
<< "\nAverage of average temperatures is: " << avgsum << '\n';
waitForEnter();
return 0;
}
void waitForEnter()
{
std::cout << "\nPress ENTER to continue...\n";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
|