I have files of daily high and low temperatures. Each day's temperature record consists of 5 integers: month day year daysHigh daysLow. program must read a record, and format and print output as shown below.
as well as the date, high, and low, print headings, print "same", "lower", or "higher" depending on the result of comparing today's temperature with yesterday's.
The data is in a file, example below. program must prompt for and read the name of the file, then process the file through eof.
must use the following 4 functions with variables:
istream& readTemperatures(istream& inFile, int &mm, int &dd, int &yy, int &low, int &high);input stream passed in, returned. function reads month, day, year, high, low from stream and passes back as parameters. return value with be false at end of file.
ostream& printHeadings(ostream& outFile); prints headings to outFile, after clearing the screen, system ("close");
ostream& printTemperatures(ostream& outFile, int mm, int dd, int yy, int high, int low ); Prints the output line. Must keep track of priorHigh, priorLow, and lineCount as static local variables. When lineCount hits 21, system("pause"); print headings and reset lineCount to zero.
string lowSameHigh( int a, int b); returns "lower", "same", or "higher" depending on whether a is less than, equal to, or greater than b.
using namespace std;
istream& readTemperatures(istream& inFile, int& mm, int& dd,
int& yy, int& high, int& low);
ostream& printHeadings(ostream& outFile);
ostream& printTemperatures(ostream& outFile, int mm, int dd, int yy, int high, int low);
string lowSameHigh(int a, int b);
//*********************************************************************
int main()
{
string inFile, fileName;
int mm, dd, yy, high, low, a, b;
cout << "Name of input file? ";
cin >> fileName;
ifstream fin("Feb2016Temp.txt");
if (!fin)
{
perror(fileName.data());
exit(1);
}
printHeadings(cout);
while (readTemperatures(fin, mm, dd, yy, high, low))
{
printTemperatures(cout, mm, dd, yy, high, low);
lowSameHigh(a, b);
}
return 0;
}
//*********************************************************************
//istream& readtemperatures(istream& inFile, int& mm, int& dd, int& yy,
//int& high, int& low); open input stream is passed in and returned.
//function reads month, day, year, high, low from stream and passes
//them back as reference parameters. The return value will be false
//on end-of-file.
//*********************************************************************
istream& readTemperatures(istream& inFile, int& mm, int& dd,
int& yy, int& high, int& low)
{
inFile >> mm >> dd >> yy >> high >> low;
return inFile;
}
//*********************************************************************
//ostream& printHeadings(ostream& outFile); prints headings to
// outFile after clearing the screen, system("cls").
//*********************************************************************
ostream& printHeadings(ostream& outFile)
{
string headings;
system("cls");
outFile << headings;
return outFile;
}
//*********************************************************************
//ostream& printTemperatures(ostream& outFile, int mm, int dd, int yy,
//int high, int low);prints the output line. must keep track of
// priorHigh, priorLow, and lineCount as static local variables. When
//lineCount hits 21, system("pause"); print heading and reset lineCount
//to zero.
//*********************************************************************
ostream& printTemperatures(ostream& outFile, int mm, int dd, int yy, int high, int low)
{
static int priorHigh, priorLow, lineCount = 0;
outFile << mm << dd << yy << high << low;
if ((lineCount == 21))
{
system("pause");
cout << mm << dd << yy << high << low;
lineCount = 0;
}
return outFile;
}
//*********************************************************************
//string lowSameHigh(int a, int b); returns "lower", "same", or
//"higher" depending on whether a is less than, equal to, or greater
// than b.
//*********************************************************************
string lowSameHigh(int a, int b)
{
if (a < b)
return "lower";
else if (a == b)
return "same";
else if (a > b)
return "higher";
}
I need help with printTemperatures() and lowSameHigh(). Can printHeadings() be deleted since the file I was given does not include the heading. the file only shows the dates and temps.
Can printHeadings() be deleted since the file I was given does not include the heading.
It looks like it is a requirement of the assignment, so I would say no you can't delete it. You need to print some headings in your output file using that function.