Hi, I am trying to create a linked list of vector in my program. And, obviously, it is not working. The program is now suffering from segmentation fault. And, by the way, I know that I could write this program without using linked list; however, as a part of my assignment, I must make a use of linked list. Here is the code for the program:
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
//Define a class to represent each Seismic Activity measurement event...
class RedwoodBaySeismicData
{
string LocationID; //This is the station that owns this piece of data…
string Date; //The date that the values were collected…
string Time; //The time of day that the values were collected…
double SeismicSize; //Magnitude of Earthquake
public:
RedwoodBaySeismicData();
RedwoodBaySeismicData(string, string, string, double);
//The first group of member functions are used to change values...
//Functions are defined in the form of inlines.
void SetID(string ID)
{
LocationID = ID;
}
void SetDate(string Day)
{
Date = Day;
}
void SetTime(string Hour)
{
Time = Hour;
}
void SetSeismicSize(double Magnitude)
{
SeismicSize = Magnitude;
}
//The second group allows controlled access to individual value in the class...
string GetID()
{
return LocationID;
}
string GetDate()
{
return Date;
}
string GetTime()
{
return Time;
}
double GetSeismicSize()
{
return SeismicSize;
}
};
//The default constructor sets the private variable members to a harmless value...
RedwoodBaySeismicData::RedwoodBaySeismicData()
{
LocationID;
Date;
Time;
SeismicSize = 0.0;
}
//This initializing constructor allows you to create a object with value already set...
RedwoodBaySeismicData::RedwoodBaySeismicData(string ID, string Day, string Hour,
double Magnitude)
{
LocationID = ID;
Date = Day;
Time = Hour;
SeismicSize = Magnitude;
}
class RedwoodBayDataList
{
vector < RedwoodBaySeismicData * > RedwoodBayDataSpots;
//Create a collection of location information...
string FileName;
int K;
public:
// void GiveRedwoodSeismicData(RedwoodBaySeismicData *X) {RedwoodBayDataSpots.push_back(X);}
RedwoodBayDataList()
{
FileName = "RedwoodBaySeismicEventList.data";
K = RedwoodBayDataSpots.size();
}
RedwoodBayDataList(RedwoodBaySeismicData);
void DisplaySeismicInfo();
void DisplayInfoForEachStation(int);
void DisplayAlphaInfo(int)
{
DisplayInfoForEachStation(K);
}
void DisplayBetaInfo(int)
{
DisplayInfoForEachStation(K);
}
void DisplayGammaInfo(int)
{
DisplayInfoForEachStation(K);
}
void DisplayDeltaInfo(int)
{
DisplayInfoForEachStation(K);
}
void DisplayEpsilonInfo(int)
{
DisplayInfoForEachStation(K);
}
};
//Allow the user to post the values for each location...
void RedwoodBayDataList::DisplaySeismicInfo()
{
string Line, ID, Date, Hour;
double Magnitude;
fstream InFile("RedwoodBaySeismicEventList.data.txt", ios::in);
string Command2;
{
cout << "Choices...................................................." <<
endl;
cout << "-----------------------------------------------------------" <<
endl;
cout <<
"Enter the name of the one of the four stations below for which you would like to have the data for seismic activity" << endl;
cout << "Alpha" << endl;
cout << "Beta" << endl;
cout << "Gamma" << endl;
cout << "Delta" << endl;
cout << "Epsilon" << endl;
cout << "-----------------------------------------------------------" <<
endl;
cout << "Command: ";
getline(cin, Command2);
}
RedwoodBaySeismicData *Buffer = new RedwoodBaySeismicData(ID, Date, Hour,
Magnitude);
cout << "\n\t============================================================="
<< endl;
//This section is the report...it's really all about the formatting...
cout << "\tWeekly Report" << endl;
cout << "\t==============================================================="
<< endl;
cout << "\tLocation\tDate\t\tHour\tMagnitude" << endl;
cout << "\t==============================================================="
<< endl;
while (getline(InFile, Line))
{
ID = strtok((char*)Line.c_str(), ":");
Date = strtok(0, ":");
Hour = strtok(0, ":");
Magnitude = atof(strtok(0, ":"));
Buffer->SetID(ID);
Buffer->SetDate(Date);
Buffer->SetTime(Hour);
Buffer->SetSeismicSize(Magnitude);
RedwoodBayDataSpots.push_back(Buffer);
if (RedwoodBayDataSpots.size() == 114)
break;
}
InFile.close();
if (Command2 == "Alpha")
{
for (K = 0; K < 23; K++)
{
DisplayAlphaInfo(K);
}
}
else if (Command2 == "Beta")
{
for (K = 23; K < 47; K++)
DisplayBetaInfo(K);
}
else if (Command2 == "Gamma")
{
for (K = 47; K < 70; K++)
DisplayGammaInfo(K);
}
else if (Command2 == "Delta")
{
for (K = 70; K < 93; K++)
DisplayDeltaInfo(K);
}
else if (Command2 == "Epsilon")
{
for (K = 93; K < 115; K++)
DisplayEpsilonInfo(K);
}
cout <<
"\t===============================================================\n\n" <<
endl;
}
void RedwoodBayDataList::DisplayInfoForEachStation(int K)
{
RedwoodBaySeismicData *Buffer;
cout << "\t" << Buffer->GetID() << "\t\t" << Buffer->GetDate() << "\t" <<
Buffer->GetTime() << "\t" << Buffer->GetSeismicSize() << endl;
}
//Declare the functions...
void RunIT();
void Menu();
void ChooseTheStation();
int main()
{
RunIT();
system("pause");
}
//This function implements a simple command processor...
void RunIT()
{
RedwoodBayDataList RedwoodBayDataSpots;
string Command1;
int K;
while (Command1 != "Quit")
{
Menu();
cout << "Command: ";
getline(cin, Command1);
if (Command1 == "Report")
RedwoodBayDataSpots.DisplaySeismicInfo();
}
}
//This function tells the user what he or she can do...
void Menu()
{
cout << "Choices...................................................." << endl;
cout << "-----------------------------------------------------------" << endl;
cout << "\tReport...displays the report." << endl;
cout << "\tQuit.....exits the program." << endl;
cout << "-----------------------------------------------------------" << endl;
}
I learned about linked list just few days ago and I am still having hard time grasping its idea. I would greatly appreciate any suggestion.