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

New Topic/Question
Reply




MultiQuote




|