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
|
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
void process(ifstream & ifile); // Prototype
int main()
{
string fname;
ifstream ifile;
char again;
do
{
cout << "Enter the input filename: ";
getline(cin, fname);
ifile.open(fname.data());
if (ifile)
{
cout << "**************************************************\n";
process(ifile);
}
else
cout << "Error opening " << fname << endl;
ifile.close();
ifile.clear();
cout << "\nDo you want to process another file?(y/n) ";
cin >> again;
cin.ignore(80,'\n');
} while (again == 'y' || again == 'Y');
return 0;
}
void process(ifstream & ifile)
{
string city; // for reading in the city name
int count1 = 0, // counts the number of temperature recorded for City 1.
count2 = 0,
intemp;
// counts the number of temperature recorded for City 2/
float temp1, // for reading in the temperature of City 1.
highest1, // for storing the highest temperature of City 1.
lowest1, // for storing the lowest temperature of City 1.
sum1 = 0, // for keeping the running total of the temperatures of City 1
average1, // for storing the average temperature of City 1.
// Below variables are for City 2
temp2,
highest2,
lowest2,
sum2 = 0,
average2;
// Pseudocode
while (!ifile.eof())
{
getline(ifile, city);
if (city == "City 1")
{
ifile >> temp1;
if (temp1 > highest1)
highest1 = temp1;
if (temp1 < lowest1)
lowest1 = temp1;
sum1 += temp1;
count1++;
}
else if (city == "City 2")
{
ifile >> temp2;
if (temp2 > highest2)
highest2 = temp2;
if (temp2 < lowest2)
lowest2 = temp2;
sum2 += temp2;
count2++;
}
getline(ifile, city);
}
average1 = sum1 / count1;
average2 = sum2 / count2;
cout << "City 1:" << endl;
cout << count1 << " " << " are recorded." << endl;
cout << fixed << setprecision(2) << "The average temperature is " << average1 << endl;
cout << fixed << setprecision(2) << "The highest temperature is " << highest1 << endl;
cout << fixed << setprecision(2) << "The lowest temperature is " << lowest1 << endl;
cout << "City 2:" << endl;
cout << count2 << " " << " are recorded." << endl;
cout << fixed << setprecision(2) << "The average temperature is " << average2 << endl;
cout << fixed << setprecision(2) << "The highest temperature is " << highest2 << endl;
cout << fixed << setprecision(2) << "The lowest temperature is " << lowest2 << endl;
}
|