taultunleashed logoc++ assignment problem : General Discussion
newtopic  postreply
 [ 3 posts ] 
blue large dot

c++ assignment problem : General Discussion

Posted: October 29th, 2005, 3:57 pm
 
cs001x
cs001x's Reps:
User avatar
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;

}


Reply with quote
Posted: October 29th, 2005, 7:28 pm
 
cs001x
cs001x's Reps:
User avatar
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...


Reply with quote
Posted: October 30th, 2005, 9:48 pm
 
wyvernx

Total Posts: 6718
Joined: May 1st, 2004, 4:00 am
wyvernx's Reps: 21
User avatar
administrator
premium
Yes, its the little things that make all the difference!


Reply with quote
Want Advertisements After The Last Post Removed? Create A Free Account!

blue large dot Who is online
Users browsing this forum: No registered users and 98 guests

Popular Sections
SWTOR Cheats
Guild Wars 2 Cheats
Guild Wars 2 Hacks
Guild Wars 2 Bots
Diablo 3 Cheats
Guild Wars 2 Mods

Popular Sections
WoW Cataclysm Cheats & Exploits
WoW Cataclysm Hacks & Bots
Star Wars The Old Republic Cheats
SWTOR Mods
Torchlight 2 Cheats
SWTOR Space Mission Bots
Site Nav and RSS
RSS Feed of General Discussion RSS Feed 
Sitemap of General Discussion Sitemap 
SitemapIndex SitemapIndex
RSS Feed RSS Feed
Channel list Channel list
left bottom corner Site and Contents Copyright 2001-2012 All Rights Reserved TaultUnleashed.com bottom corner
top left
top right
createaccount
Username:   Password:   Remember Me?