Hi, I have been dealing with the segmentation fault of this programme for a very long time. And, I can't seem to find what exactly is the problem. I was wondering if anyone could help me get rid of this problem. The code for the programme is below:
//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
class RedwoodBayDataList {
vector<RedwoodBaySeismicData> RedwoodBayDataSpots; //Create a collection of location information...
string FileName ;
public:
RedwoodBayDataList() {FileName = "RedwoodBaySeismicEventList.Data";}
void PostSeismicInfo();
void WeeklyReport();
};
//Allow the user to post the values for each location...
void RedwoodBayDataList::PostSeismicInfo()
{
int K;
string Line, ID, Date, Hour;
double Magnitude;
fstream InFile("RedwoodBaySeismicEventList.data", ios::in);
//...then get the values for each location...
RedwoodBaySeismicData Buffer;
for(K = 0 ; K < 69 ; K++) {
getline(InFile, Line);
ID = Line.substr(0,4);
Date = Line.substr(6,7);
Hour = Line.substr(9,3);
Magnitude = atof(strtok((char*)Line.c_str(), " ") );
Buffer.SetID(ID); //Use these values to load the Buffer record...
Buffer.SetDate(Date);
Buffer.SetTime(Hour);
Buffer.SetSeismicSize(Magnitude);
RedwoodBayDataSpots.push_back(Buffer);
If you run it with a debugger it will tell you the line that causes the segFault, and let you check the values of each variables to see which one is causing the problem.
for(K = 0 ; K < 69 ; K++) {
getline(InFile, Line);
ID = Line.substr(0,4);
Date = Line.substr(6,7);
Hour = Line.substr(9,3);
Magnitude = atof(strtok((char*)Line.c_str(), " ") );
You have lots of magic numbers there. What do they mean? Are they correct?
You have a call to strtok() without checking the result.
Did you know that strtok() modifies the buffer? You're passing in a string which allows users to see a const representation. You are then casting away that const to satisfy strtok(), but that's a serious error too.
Sorry guys, I am new to this forum. So, I did not know how to submit the code for the programme in a proper way. Anyway, I am submitting my code again:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
usingnamespace 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 ;
public:
RedwoodBayDataList() {FileName = "RedwoodBaySeismicEventList.Data";}
void PostSeismicInfo();
void WeeklyReport();
};
//Allow the user to post the values for each location...
void RedwoodBayDataList::PostSeismicInfo()
{
int K;
string Line, ID, Date, Hour;
double Magnitude;
fstream InFile("RedwoodBaySeismicEventList.data", ios::in);
//...then get the values for each location...
RedwoodBaySeismicData Buffer;
for(K = 0 ; K < 69 ; K++) {
getline(InFile, Line);
ID = Line.substr(0,4);
Date = Line.substr(6,7);
Hour = Line.substr(9,3);
Magnitude = atof(strtok((char*)Line.c_str(), " ") );
Buffer.SetID(ID); //Use these values to load the Buffer record...
Buffer.SetDate(Date);
Buffer.SetTime(Hour);
Buffer.SetSeismicSize(Magnitude);
RedwoodBayDataSpots.push_back(Buffer);
RedwoodBayDataSpots[K].SetID(ID);
RedwoodBayDataSpots[K].SetDate(Date);
RedwoodBayDataSpots[K].SetTime(Hour);
RedwoodBayDataSpots[K].SetSeismicSize(Magnitude);
cin.ignore(100,'\n'); //This flushes the input buffer of any extra characters.
}
}
//This produces the final product...the report on Seismic Activiies.....
void RedwoodBayDataList::WeeklyReport()
{
int Count;
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\tHour\tMagnitude" << endl;
cout << "\t===============================================================" << endl;
for(Count = 0 ; Count < 69 ; Count++)
cout << "\t" << RedwoodBayDataSpots[Count].GetID() << "\t" << RedwoodBayDataSpots[Count].GetDate() << "\t" << RedwoodBayDataSpots[Count].GetTime() << "\t" << RedwoodBayDataSpots[Count].GetSeismicSize();
cout << "\t===============================================================\n\n" << endl;
}
//Declare the functions...
void RunIT();
void Menu();
int main()
{
RunIT();
}
//This function implements a simple command processor...
void RunIT()
{
RedwoodBayDataList RedwoodBayDataSpots;
string Command = "";
while(Command != "Quit") {
Menu();
cout << "Command: ";
getline(cin, Command);
if(Command == "Report")
RedwoodBayDataSpots.WeeklyReport();
}
}
//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;
}
Hi Coder777. Thanks for your response. Do you happen to know the way of reading the "Magnitude" variable from the file without using "strtok". The thing is that since "Magnitude" is a double variable I can't use the "substr" like I did for other variables in order to read it from the file