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
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int MAX = 12;
struct Temperature
{
string month;
int high;
int low;
};
int loadData(ifstream &inFile, Temperature temps[], int &rows);
Temperature findHigh(Temperature temps[], int rows, int &highest);
Temperature findLow(Temperature temps[], int rows, int &lowest);
int main()
{
Temperature temps[MAX];
ifstream inFile;
int rows = 0;
int j = 0;
int highest = 0;
int lowest = 0;
loadData(inFile, temps, rows);
for (j = 0; j < rows; j++)
{
cout << temps[j].month << " "
<< temps[j].high << " "
<< temps[j].low << endl;
}
findHigh(temps, rows, highest);
cout << "The high temp was " << temps[highest].high << " in "
<< temps[highest].month << endl;
findLow(temps, rows, lowest);
cout << "The low temp was " << temps[lowest].low << " in "
<< temps[lowest].month << endl;
return 0;
}
int loadData(ifstream &inFile, Temperature temps[], int &rows)
{
int i;
inFile.open("temps.txt");
for (i = 0; i < MAX; i++)
{
inFile >> temps[i].month >> temps[i].high >> temps[i].low;
rows++;
}
cout << "File open" << endl;
cout << rows << " rows counted." << endl;
}
Temperature findHigh(Temperature temps[], int rows, int &highest)
{
int index = 0;
for (index = 0; index < rows; index ++)
{
if (temps[index].high > temps[highest].high)
temps[highest].high = temps[index].high;
}
}
Temperature findLow(Temperature temps[], int rows, int &lowest)
{
cout << "findLow called" << endl;
int index = 0;
for (index = 0; index < rows; index++)
{
if (temps[lowest].low > temps[index].low)
temps[lowest].low = temps[index].low;
}
}
|