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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>
#include <cstdlib>
using namespace std;
int i;
string input =" ";
struct weather
{
double rainfall;
double high;
double low;
};
void readWeather(double &Vrainfall, double &Vhigh, double &Vlow, int &i);
void CalcAvgRainTemp(weather months[], double &vTotal, double &VAvgRainfall ,double &VAvgTemp);
void calHighLow(weather months[], double &vhighest, int &vhighMon, double &vlowest, int &vlowMon);
void readWeather(double &Vrainfall, double &Vhigh, double &Vlow, 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 >> Vrainfall)
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 >> Vhigh)
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 >> Vlow)
break;
}
}
}
void CalcAvgRainTemp(weather months[], double &vTotal, double &VAvgRainfall , double &VAvgTemp)
{
double TotalHigh=0.0;
double TotalLow=0.0;
for (int x = 0; x < 3; x++) // 3 for testing 12 for full
{
vTotal += months[x].rainfall ;
}
VAvgRainfall = vTotal / 3; // 3 for testing 12 for full
for (int y=0; y < 3; y++ ) // 3 for testing 12 for full
{
double &vhighest = months[y].high;
double &vlowest = months[y].low;
TotalHigh += months[y].high;
TotalLow += months[y].low;
}
VAvgTemp = (TotalHigh + TotalLow) / 3; // 3 for testing 12 for full
}
void calHighLow(weather months[], double &vhighest, int &vhighMon, double &vlowest, int &vlowMon)
{
vhighest = months[0].high;
for (int x = 0; x < 3; x++) // 3 for testing 12 for full
{
if (months[x].high >= vhighest )
{
vhighest = months[x].high;
vhighMon = x+1;
}
}
vlowest = months[0].low;
for (int y = 0; y < 3; y++) // 3 for testing 12 for full
{
if (months[y].low <= vlowest )
{
vlowest = months[y].low;
vlowMon = y+1;
}
}
}
int main()
{
cout << "----------------- Weather Statistics -------------------" << endl;
cout << " " << endl;
weather months[3]; // 3 for testing 12 for full
double Vrainfall, Vhigh, Vlow, vhighest, vlowest;
double vTotal = 0, VAvgRainfall = 0, VAvgTemp = 0, C = 0;
int vhighMon = 0, vlowMon = 0;
for (int i = 0; i < 3; i++) // 3 for testing 12 for full
{
readWeather(Vrainfall, Vhigh, Vlow, i);
months[i].rainfall = Vrainfall;
months[i].high= Vhigh;
months[i].low = Vlow;
}
CalcAvgRainTemp ( months, vTotal, VAvgRainfall, VAvgTemp);
calHighLow ( months, vhighest, vhighMon, vlowest, vlowMon);
// Display total rainfall.
cout << " " << endl;
cout << "Total Rainfall: "<<setw(3)<<vTotal<<endl;
// Display the average rainfall for the year
cout << "Average Monthly Rain: "<<setw(3)<<VAvgRainfall<<endl;
// Display the average temperature for the month
cout << "Average Monthly Average Temperature: "<<setw(3)<<VAvgTemp<<endl;
// Display the highest temperature for the year
cout << "Highest Temperature: "<<setw(3)<<vhighest<<" in month "<<setw(2)<<vhighMon<<endl;
// Display the lowest temperature for the year
cout << "Lowest Temperature: " <<setw(3)<<vlowest<<" in month "<<setw(2)<<vlowMon<<endl;
system("pause");
return 0;
}
/*
----------------- Weather Statistics -------------------
Enter month 1 total rainfall : 4.55
Enter month 1 high temperature : 99.7
Enter month 1 low temperature : 56.8
Enter month 2 total rainfall : 3.23
Enter month 2 high temperature : 105.7
Enter month 2 low temperature : 33.5
Enter month 3 total rainfall : 4.23
Enter month 3 high temperature : 100.4
Enter month 3 low temperature : 29.5
Total Rainfall: 12.01
Average Monthly Rain: 4.00333
Average Monthly Average Temperature: 141.867
Highest Temperature: 105.7 in month 2
Lowest Temperature: 29.5 in month 3
Press any key to continue . . .
*/
|