Post Reply Home » Forums » General » General Discussion

c++ assignment problem : General Discussion

Posted: October 29th, 2005
cs001x
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:

Code: Select all

#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;

}
Posted: October 29th, 2005
cs001x
My professor just emailed me back with a reply, it had been several hours since the last email and I'd given up that's why I posted here. Anyways, this is all I needed him to say and it made the assignment very easy:

"See, once you've extracted data from an input stream, the data is no longer there."

It's the little things that kick my !@#$%^&* so bad like that...
Posted: October 30th, 2005
User avatar
Premium
Total Posts:6718 Joined:2004
Yes, its the little things that make all the difference!
Ready to join the community? Click here and see all of the benefits!
blue large dotWho is online
Users browsing this forum: No registered users and 41 guests
Post Reply