I was hoping someone here could help me out with an assignment in c++. The concept is simple, it reads in a csv file, supplied by the national weather service, with the following format:
2005,7
1,85,67,70
2,79,60,70
3,80,54,70
4,83,60,70
My program needs to read in this data, then output it with a set format. All I need answered is what is the correct way to read in the data, and output it correctly. I already know how to format the data once I have it, I'm just having a really hard time getting it.
Report for year/month: 2005/07
All temperatures in Fahrenheit
Day | Low | High | Ave | Depart
1 | 85 | 67 | 76 | 6.0
2 | 79 | 60 | 70 | -0.5
3 | 80 | 54 | 67 | -3.0
4 | 83 | 60 | 72 | 1.5
Here is the C++ code I have so far:
(!empty($user->lang['CODE'])) ? $user->lang['CODE'] : ucwords(strtolower(str_replace('_', ' ', 'CODE'))):
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <iomanip>
using namespace std;
struct TempData
{
int day;
int low;
int high;
int normal;
};
void generate_report(ifstream& fin, ostream& out);
bool read_record(ifstream& fin, TempData& t);
void write_record(ostream& out, TempData& t);
int main()
{
ifstream mainfin;
ofstream mainfout;
char infilename[20];
char outfilename[20];
char sorfanswer;
cout << "Welcome to the temperature report generator\n";
cout << "Enter the name of the temperature data file: ";
cin >> infilename;
mainfin.open(infilename);
if (mainfin.fail( ))
{
cout << "Input file opening failed.\n";
exit(1);
}
do
{
cout << "Send report to screen or to a file? ";
cin >> sorfanswer;
} while (! (toupper(sorfanswer) == 'S' || toupper(sorfanswer) == 'F'));
if (toupper(sorfanswer) == 'F')
{
cout << "Enter the name of the file to store the report: ";
cin >> outfilename;
mainfout.open(outfilename);
if (mainfout.fail( ))
{
cout << "Output file opening failed.\n";
exit(1);
}
generate_report(mainfin,mainfout);
cout << "Report written to file \"" << outfilename << "\"\n";
mainfout.close();
}
else if (toupper(sorfanswer) == 'S')
{
generate_report(mainfin,cout);
}
mainfin.close();
cout << "\nReport generation completed.\n";
return 0;
}
void generate_report(ifstream& fin, ostream& out)
{
TempData t;
while(read_record(fin,t))
{
write_record(out,t);
}
}
bool read_record(ifstream& fin, TempData& t)
{
char ch;
fin >> t.day >> ch;
fin >> t.low >> ch;
fin >> t.high >> ch;
fin >> t.normal;
return (!fin.eof());
}
void write_record(ostream& out, TempData& t)
{
out << t.day << "," << t.low << "," << t.high << "," << t.normal << endl;
}