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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>
#include <cstdlib>
using namespace std;
int i;
string input = "";
struct weather
{
double totRain;
double highTemp;
double lowTemp;
};
void readWeather(double &vRfall, double &vHtemp, double &vLtemp, int &i);
void calTotAvg(weather months[], double &vTotal, double &vAverage);
void calHighLow(weather months[], double &vhighest, int &vhighMon, double &vlowest, int &vlowMon);
int main()
{
cout << "---------------------- Weather Statistics --------------------------" << endl;
cout << " " << endl;
weather months[12];
double vRfall, vHtemp, vLtemp;
for (int i = 0; i < 12; i++)
{
readWeather(vRfall, vHtemp, vLtemp, i);
months[i].totRain = vRfall;
months[i].highTemp= vHtemp;
months[i].lowTemp = vLtemp;
}
double vTotal, vAverage;
calTotAvg ( months, vTotal, vAverage);
int vhighMon, vlowMon;
double vhighest, vlowest;
calHighLow ( months, vhighest, vhighMon, vlowest, vlowMon);
// Display total rainfall.
cout << " " << endl;
cout << " The total rainfall for the year : " << setw(3) << vTotal << endl;
// Display the average rainfall for the year
cout << " The average rainfall for the year : " << setw(3) << vAverage << endl;
// Display the highest temperature for the year
cout << " The highest temperature for the year : " << setw(3) << vhighest << " in month " << setw(2) << vhighMon << endl;
// Display the lowest temperature for the year
cout << " The lowest temperature for the year : " << setw(3) << vlowest << " in month " << setw(2) << vlowMon << endl;
return 0;
}
void readWeather(double &vRfall, double &vHtemp, double &vLtemp, int &i)
{
cout << " " << endl;
while (true)
{
cout << " Enter month " << setw(2) << (i+1) << " total rainfall : " ;
cin >> input;
if ( input.find_first_not_of("1234567890.") != string::npos )
{
cout << " * Invalid input, please try again" << '\a' << endl;
}
else
{
stringstream myStream(input);
if (myStream >> vRfall)
break;
}
}
while (true)
{
cout << " Enter month " << setw(2) << (i+1) << " high temperature : " ;
cin >> input;
if ( input.find_first_not_of("1234567890.") != string::npos )
{
cout << " * Invalid input, please try again" << '\a' << endl;
}
else
{
stringstream myStream(input);
if (myStream >> vHtemp)
break;
}
}
while (true)
{
cout << " Enter month " << setw(2) << (i+1) << " low temperature : " ;
cin >> input;
if ( input.find_first_not_of("1234567890.") != string::npos )
{
cout << " * Invalid input, please try again" << '\a' << endl;
}
else
{
stringstream myStream(input);
if (myStream >> vLtemp)
break;
}
}
}
void calTotAvg(weather months[], double &vTotal, double &vAverage)
{
for (int x = 0; x < 12; x++)
{
vTotal += months[x].totRain ;
}
vAverage = vTotal / 12;
}
void calHighLow(weather months[], double &vhighest, int &vhighMon, double &vlowest, int &vlowMon)
{
vhighest = months[0].highTemp;
for (int x = 0; x < 12; x++)
{
if (months[x].highTemp >= vhighest )
{
vhighest = months[x].highTemp;
vhighMon = x+1;
}
}
vlowest = months[0].lowTemp;
for (int y = 0; y < 12; y++)
{
if (months[y].lowTemp <= vlowest )
{
vlowest = months[y].lowTemp;
vlowMon = y+1;
}
}
}
|