Class catastrophe

Need a little help here

Page 1 of 1

6 Replies - 371 Views - Last Post: 16 March 2010 - 03:46 PM Rate Topic: -----

#1 jessicalegner  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 66
  • Joined: 05-June 09

Class catastrophe

Posted 16 March 2010 - 10:45 AM

Hello,
I have a program that I am very proud of so far, as a beginner at least.
It deals with multiple classes and many other things.
My questions are many, but here is just one to start:

1. I want to write a line of output (including the warning, starting time, ending time...) from my alert object, not the driver. I have attempted, but I can't get it to function at all. Hoping someone can assist me with this.

Driver
//***************************************************************
// This program reads a line from the file alerts.txt such as
// (2009-09-13T20:16,2009-09-13T21:15,26-145,SEVERE THUNDERSTORM WARNING)
// then returns the state abreviation, county name, start and end time 
// and then writes the items to the console.  Should look like
// (SEVERE THUNDERSTORM WARNING for Saginaw County, MI
// Effective: 13 SEP 09 at 8:16 p.m. until 13 SEP 09 9:15 p.m.)
//***************************************************************

#include <iostream>
#include <fstream>
#include <cmath>
#include <string>
using namespace std;

#include "countyManager.h"
#include "alerts.h"
#include "StringTokenizer.h"

int main()
{
    ifstream AlertFile;         // Input file identifier    
    int index;                  // Search outcome                                                                
    string targetFips;          // FIPS code of desired county
    county outFips;             // To store FIPS code info for desired code
	string alertString;			// String to hold one line of alert from file
	int i = 0,					
		numAlerts;				// number of alerts in file
	string startTime,			// used to hold exracted tokens fom the alert string
		   endTime,				// "
	       AFIPS,				// "
	       warning;				// "
                    
    // Declare county list object - will build from file
    countyManager countyDataSet;

	// Declare alert list object - will build from file
	alerts AlertDataSet;
	
	// Open and verify file existence
    AlertFile.open("alerts.txt");   
    if (AlertFile.fail() )        
    {
      cout <<  "Problem opening file";
      exit(-1);
    }
	getline(AlertFile, alertString);
	while (!AlertFile.eof())        
    {
		StringTokenizer alertTokenString(alertString);         // New alert StringTokenizer object

		// Extract first token as the starting time and date                                             
        startTime = alertTokenString.getStringToken();

		// Extract first token as the ending time and date                                           
        endTime = alertTokenString.getStringToken();

		// Extract first token as fips code from alert                                         
        AFIPS = alertTokenString.getStringToken();

		// Extract first token as cwarning type                                          
        warning = alertTokenString.getStringToken();

        AlertDataSet.setValues(startTime, endTime, AFIPS, warning);  // Build object
		
		targetFips = AFIPS;
		// Search for index of desired FIPS code
		index = countyDataSet.searchForFIP(targetFips);
    
		if (index >= 0)
		{
			// Retrieve county object for the alert line
			outFips = countyDataSet.getFIPInfo(index);
                                                                 
			// Write output
			cout << endl << endl;
			cout << AlertDataSet.getWarning() << " for " << outFips.getCountyName() << ", ";
			cout << outFips.getStateAbrv() 	  << endl;
			cout << "Effective: " ;      
		}
		i++;
		getline(AlertFile,alertString);			// Continuation read                
    }            
    numAlerts = i;
	cout << numAlerts << " alerts have been printed.";  
    // Close file
    AlertFile.close();    
    
	system("pause");
    return 0;                
}



alerts.h
// This class defines the data and operations required to manage
// information related to an alert from file

#include <string>
using namespace std;

// Record structure to store attributes of one alert
class alerts
{
  private:
    string startTime;
	string endTime;
	string Alert_fipsCode;
	string warningType;
    
  public:
     // Set default values
     alerts();         
     
     // Set all parameters for alert
     void setValues(string st, string et, string fc, string w);
     
     // Functions to get individual attributes of alert
     string getStartTime();
	 string getEndTime();
	 string getAlertFips();
	 string getWarning();
};



alerts.cpp
// This file implements functions related to the alert class

#include <string>
using namespace std;

#include "alerts.h"

// Default constructor
alerts::alerts()
{
   startTime = "";
   endTime = "";
   Alert_fipsCode = "";
   warningType = "";
};

// "Set" function to build complete object at once
void alerts::setValues(string st, string et, string fc, string w)   
{
   startTime = st;
   endTime = et;
   Alert_fipsCode = fc;
   warningType = w;
};

// "Get" functions for retrieving data
string alerts::getStartTime()
{
   return startTime;
}

string alerts::getEndTime()
{
   return endTime;
}

string alerts::getAlertFips()
{
   return Alert_fipsCode;
}

string alerts::getWarning()
{
   return warningType;
}



Is This A Good Question/Topic? 1
  • +

Replies To: Class catastrophe

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6037
  • View blog
  • Posts: 23,432
  • Joined: 23-August 08

Re: Class catastrophe

Posted 16 March 2010 - 11:14 AM

So I'm supposing you really want to add the operators necessary to do this?

alerts alertsObj;
cout << alertsObj;


EDIT: In which case this tutorial looks pretty good[/url]
Was This Post Helpful? 0
  • +
  • -

#3 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Class catastrophe

Posted 16 March 2010 - 01:14 PM

just to add to what Jack said, heres an example of how to overload the << operator
#include <iostream>
#include <string>

using namespace std;


class Person
{
	string name,
		   dob,
		   address;
public:
	Person(string name, string dob, string address)
		:name(name),dob(dob),address(address)
	{}

	//the friend operator tells this function that it has access to 
	//all the private members, but its not a member function of this class
	friend ostream &operator<<(ostream &stream , Person &obj);
};

//define the function
ostream &operator<<(ostream &stream, Person &obj)
{
  stream<<obj.name<<endl
		<<obj.dob<<endl
		<<obj.address;
  return stream; //return the output stream
}

int main()
{
	Person me("Justin","02/09/1990","1234 Avenue J");
	cout<<me<<endl;


	cin.ignore();
	cin.get();
	return 0;
}





Was This Post Helpful? 0
  • +
  • -

#4 jessicalegner  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 66
  • Joined: 05-June 09

Re: Class catastrophe

Posted 16 March 2010 - 01:16 PM

What I am looking to do is have basically NOTHING in main except the things that are necassary. I want to keep the code that I have in main, I just want it as a member function of the alert class.
I would like to put all of this
    // Open and verify file existence
    AlertFile.open("alerts.txt");   
    if (AlertFile.fail() )        
    {
      cout <<  "Problem opening file";
      exit(-1);
    }
        getline(AlertFile, alertString);
        while (!AlertFile.eof())        
    {
                StringTokenizer alertTokenString(alertString);         // New alert StringTokenizer object

                // Extract first token as the starting time and date                                             
        startTime = alertTokenString.getStringToken();

                // Extract first token as the ending time and date                                           
        endTime = alertTokenString.getStringToken();

                // Extract first token as fips code from alert                                         
        AFIPS = alertTokenString.getStringToken();

                // Extract first token as cwarning type                                          
        warning = alertTokenString.getStringToken();

        AlertDataSet.setValues(startTime, endTime, AFIPS, warning);  // Build object
                
                targetFips = AFIPS;
                // Search for index of desired FIPS code
                index = countyDataSet.searchForFIP(targetFips);
    
                if (index >= 0)
                {
                        // Retrieve county object for the alert line
                        outFips = countyDataSet.getFIPInfo(index);
                                                                 
                        // Write output
                        cout << endl << endl;
                        cout << AlertDataSet.getWarning() << " for " << outFips.getCountyName() << ", ";
                        cout << outFips.getStateAbrv()    << endl;
                        cout << "Effective: " ;      
                }
                i++;
                getline(AlertFile,alertString);                 // Continuation read                
    }            
    numAlerts = i;
        cout << numAlerts << " alerts have been printed.";  
    // Close file
    AlertFile.close();    

into my alert class.
The problem is that I don't know how to do that. I don't know what I need to keep in the driver, and what to put into the alert class. I tried just copy and pasting what I thought I needed, and making it a function, but that did NOT work out for me what so ever.

I want to be able to call the function from the alert class in my driver from the alert class. Not to have the driver doing all of the work.
Was This Post Helpful? 0
  • +
  • -

#5 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Class catastrophe

Posted 16 March 2010 - 01:32 PM

would somthing like this work?
void doStuff(const char *file, alerts &AlertDataSe)
{
	ifstream alertFile(file);
	if(alertFile)
	{
		cerr<<"File cannot be opened"<<endl;
		return;
	}
	string targetFips;          // FIPS code of desired county
	county outFips;  

	int index, i = 0;
	string alertString, startTime, endTime, AFIPS, warning;
	getline(AlertFile,alertString);

	while (!AlertFile.eof())        
	{
		StringTokenizer alertTokenString(alertString);         // New alert StringTokenizer object

		// Extract first token as the starting time and date                                             
		startTime = alertTokenString.getStringToken();

		// Extract first token as the ending time and date                                           
		endTime = alertTokenString.getStringToken();

		// Extract first token as fips code from alert                                         
		AFIPS = alertTokenString.getStringToken();

		// Extract first token as cwarning type                                          
		warning = alertTokenString.getStringToken();

		AlertDataSet.setValues(startTime, endTime, AFIPS, warning);  // Build object

		targetFips = AFIPS;
		// Search for index of desired FIPS code
		index = countyDataSet.searchForFIP(targetFips);

		if (index >= 0)
		{
			// Retrieve county object for the alert line
			outFips = countyDataSet.getFIPInfo(index);

			// Write output
			cout << endl << endl;
			cout << AlertDataSet.getWarning() << " for " << outFips.getCountyName() << ", ";
			cout << outFips.getStateAbrv()    << endl;
			cout << "Effective: " ;      
		}
		i++;
		getline(AlertFile,alertString);                 // Continuation read       
	}
	numAlerts = i;
	cout << numAlerts << " alerts have been printed.";  
	// Close file
	AlertFile.close();
}


This post has been edited by ImaSexy: 16 March 2010 - 01:32 PM

Was This Post Helpful? 1
  • +
  • -

#6 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: Class catastrophe

Posted 16 March 2010 - 01:39 PM

Hi Jessica,

I think I have got a rough idea of what you are after. What you need to do (or at least what I would do), is to make ifstream a base of the alerts class. The whole of the reading of the alerts file can then be shifted into the alerts class.

Is that the kind of thing you are after?
Was This Post Helpful? 1
  • +
  • -

#7 jessicalegner  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 66
  • Joined: 05-June 09

Re: Class catastrophe

Posted 16 March 2010 - 03:46 PM

I think so. I think I am going to try what ImaSexy and Martyn.Rae suggested-See if that helps.
As far as overloading the operator...that is BEYOND me at this point in time. My prof went over it a few weeks ago, and I was LOOOOOSTTTTTTTT still have NOT caught on yet :( I need to do some self motivated research on the topic, once I get motivated to do so ;). It is hard to make time for other things other than course work, even if it would be beneficial. College....YUCK!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1