read file using function with reference variables , parameters

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.

example of how Output should look:
Date Daily Compared Daily Compared
High to Prior Low to Prior
2/01/2016 54 39
2/02/2016 66 higher 42 higher
2/03/2016 50 lower 30 lower
2/04/2016 43 lower 22 lower
2/05/2016 42 lower 27 higher
2/06/2016 58 higher 28 higher
2/07/2016 55 lower 38 higher
2/08/2016 40 lower 23 lower
2/09/2016 26 lower 19 lower
2/10/2016 24 lower 15 lower
2/11/2016 32 higher 23 higher
2/12/2016 33 higher 18 lower
2/13/2016 24 lower 11 lower
2/14/2016 31 higher 22 higher
2/15/2016 42 higher 29 higher
2/16/2016 41 lower 33 higher
2/17/2016 50 higher 36 higher
2/18/2016 61 higher 35 lower
2/19/2016 77 higher 49 higher
2/20/2016 78 higher 48 lower
2/21/2016 59 lower 40 lower
Last edited on
You'll need to show what you've done so far, then someone can help.


#include <iostream>
#include <fstream>
#include <string>

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";
}
Do you have a question or problem?

Also use code tags when posting code.

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.

I need help with printTemperatures()

Such as? You need to ask specific questions.

are the functions going to show the expected output?
Since you never use the variables you created in the printTemperature() function, no I don't think you'll get the output you're expecting.

By the way did you compile and run the program? If so you should know if the function is producing the desired output.


Topic archived. No new replies allowed.