c storing parsed data into class I have to parse through a d
c++ storing parsed data into class.
I have to parse through a data read in from a file, and have the parsed data be stored in a class. There are 3 variables total, and I have parsed the data so I do have three variables held in a temporary string. I need to put them into a class.
*relevant details*
-I am using self-implemented data structure and no container from STL. Given this I can only #include<iostream, fstream, cstring> and of course other <> that is not of STL container.
Here is the .h and .cpp file that I have.
//.h
#ifndef endLocation
#define endLocation
#include \"myLinkedlist.h\"
#include \"myString.h\"
class endLocation
{
private:
String locationName;
int travelTime; // will use atoi
int travelCost; // will use atoi
public:
endLocation();
endLocation(String, int, int);
endLocation(endLocation& rhs);
void setName(String);
void setTime(int);
void setCost(int);
String getName();
int getTime();
int getCost();
};
#endif // endLocation
//.cpp implementation
endLocation::endLocation()
{
locationName = \"\";
travelTime = 0;
travelCost = 0;
}
endLocation::endLocation(String inputName, int inputTime, int inputCost)
{
locationName = inputName;
travaelTime = inputTime;
travelCost = inputCost;
}
endLocation::endLocation(endLocation& rhs)
{
locationName = rhs.locationName;
travelTime = rhs.travelTime;
travelCost = rhs.travelCost;
}
void endLocation::setName(String inputName)
{
locationName = inputName;
}
void endLocation::setTime(int inputTime)
{
travelTime = inputTime;
}
void endLocation::setCost(int inputCost)
{
travelCost = inputCost;
}
String endLocation::getName()
{
return locationName;
}
int endLocation::getTime()
{
return travelTime;
}
int endLocation::getCost()
{
return travelCost;
}
Here is my main
int main( int argc, char* const argv[] )
{
if(argc > 1){
ifstream in(argv[1]);
if(in.is_open()){
int num_lines, index, size;
char line[200];
char name[100];
char time[100];
char cost[100];
in >> num_lines; // take number of lines here
for(int i = 0; i < num_lines; ++i){
in >> line; // Take a line
// initialize all strings
memset(name, \'\\0\', sizeof(name));
memset(time, \'\\0\', sizeof(time));
memset(cost, \'\\0\', sizeof(cost));
// parse and store all strings
size = findIndex(line, \'|\');
strncpy(name, line, size);
index = size + 1;
size = findIndex(line + index, \'|\');
strncpy(time, line + index, size);
index += size + 1;
size = findIndex(line + index, \'|\');
strncpy(cost, line + index, size);
}
in.close();
}
else{
cout << \"can not open \" << argv[1] << \" to read\" << endl;
}
}
return 0;
}
I have the file read in, and stored. I need to pass them as parameters on my constructor for my endLocation class. What would be the syntax for this? Could you give an example of how you would add to main to make this happen, and the compiled results?
Thank you for your time.
Solution
#ifndef endLocation
#define endLocation
#include \"myLinkedlist.h\"
#include \"myString.h\"
class endLocation
can use atoi
int travelCost; // can use atoi
public:
endLocation();
endLocation(String, int, int);
endLocation(endLocation& rhs);
void setName(String);
void setTime(int);
void setCost(int);
String getName();
int getTime();
int getCost();
};
#endif // endLocation
//.cpp implementation
endLocation::endLocation()
endLocation::endLocation(endLocation& rhs)
void endLocation::setName(String inputName)
void endLocation::setTime(int inputTime)
void endLocation::setCost(int inputCost)
String endLocation::getName()
come locationName;
}
int endLocation::getTime()
come travelTime;
}
int endLocation::getCost()
come travelCost;
}
Here is my main
int main( int argc, char* const argv[] )
range of lines here
for(int i = 0; i < num_lines; ++i)dissect and store all strings
size = findIndex(line, \'|\');
strncpy(name, line, size);
index = size + 1;
size = findIndex(line + index, \'|\');
strncpy(time, line + index, size);
index += size + 1;
size = findIndex(line + index, \'|\');
strncpy(cost, line + index, size);
}
in.close();
}
else



