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
|
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
istream& readTemperatures(istream&, int&, int&, int&, int&, int&);
ostream& printHeadings(ostream&);
ostream& printTemperatures(ostream&, int, int, int, int, int);
string lowSameHigh(int, int);
//*********************************************************************
int main()
{
string fileName;
int month,
day,
year,
high,
low;
cout << "Name of input file? ";
cin >> fileName;
ifstream fin(fileName);
if (!fin)
{
perror(fileName.data());
exit(1);
}
system("cls");
printHeadings(cout);
while (readTemperatures(fin, month, day, year, high, low))
{
printTemperatures(cout, month, day, year, high, low);
}
return 0;
}
//********************************************************************
// return mm, dd, yy, high, and low as reference parameters
// input stream as return value
istream& readTemperatures(istream& inFile, int& mm, int& dd, int& yy, int& high,
int& low)
{
inFile >> mm >> dd >> yy >> high >> low;
return inFile;
}
//********************************************************************
// print headings to outFile, after clearing the screen
ostream& printHeadings(ostream& outFile)
{
outFile << fixed << left << "Date" << right
<< setw(20) << "Daily High" << right
<< setw(20) << "Compared to Prior" << right << setw(20)
<< "Daily Low" << right << setw(20) << "Compared to Prior" << endl;
return outFile;
}
//********************************************************************
// print output line and keeps track of priorHigh, priorLow, and lineCount
// when lineCount hits 21 resets lineCount to zero.
ostream& printTemperatures(ostream& outFile, int mm, int dd, int yy,
int high, int low)
{
static int priorHigh = 0,
priorLow = 0,
lineCount = 1;
outFile << fixed << setprecision(0) << left << mm << "/";
if (dd < 10)
{
cout << "0";
}
outFile << dd << "/" << yy << right << setw(15) << high << setw(20);
lowSameHigh(high, priorHigh);
priorHigh = high;
outFile << setw(20) << low << setw(20);
lowSameHigh(low, priorLow);
priorLow = low;
outFile << endl;
if (lineCount < 21)
{
++lineCount;
}
else
{
system("pause");
system("cls");
lineCount = 0;
printHeadings(cout);
}
return outFile;
}
//********************************************************************
// returns lower, same, or higher depending on wether value is less,
// equal to, or greater than previous value
string lowSameHigh(int a, int b)
{
string status;
if (a < b)
{
cout << "lower";
}
else if (a = b)
{
cout << "same";
}
else (a > b);
{
cout << "higher";
}
return status;
}
|