0

I am a C++ beginner. My basic code shown below is able to read csv file and print out data line by line. However, I cannot figure out how to assign all these data to my defined commuter class variables. I have tried alot of online tutorial but it will always show errors that I am not sure how to debug.Could anyone give me a hand or some hints? thank you.

sample data:

commuter1;A;7;20 
commuter2;B;8;30
commuter3;F;10;10
.....
#include<iostream>
#include<string>
#include<vector>
#include<fstream>//strtok 
using namespace std;
class Commuter {
private:
    string name;
    char point;
    int hour;
    int minute;
public:
    Commuter(string name, char point, int hour, int min) {
        this->name = name;
        this->point = point;
        this->hour = hour;
        this->minute = min;
}
void setname(string name);
void setpoint(char point);
void sethour(int hour);
void setmin(int min);
vector<Commuter> commuter;
};

void Commuter::setname(string name) {
this->name = name;
}
void Commuter::setpoint(char point) {
this->point = point;
}
void Commuter::sethour(int hour) {
this->hour = hour;
}
void Commuter::setmin(int min) {
this->minute = min;
}
int main() {
ifstream commuterfile;
string filename;
string str;
cout << "Enter the file path: " << endl;
cin >> filename;
commuterfile.open(filename.c_str());
if (!commuterfile) {
    cerr << "ERROR" << endl;
    exit(1);
}
while (getline(commuterfile, str, ';')) {
    cout << str << endl;
}
commuterfile.close();
return 0;
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
YUCHEN XI
  • 1
  • 1
  • 2
    What do you mean with "show errors"? We are not mind-readers, you have to *tell* us the errors you get. Do you get them when building? When running? Does your program crash? Show unexpected output? Please take some time to [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask), and then *edit your question* to include more details. – Some programmer dude Apr 01 '17 at 11:09

1 Answers1

0

You have the line of data in your str variable, so you should be able to take a few more steps and add that to your class.

First, you should figure out how to split the string into components.

The next step is to convert each component from a string to the data type you need. atoi will be a good function for converting string to int.

You need an instance of your class as well. Eg Commuter commuter; Then you can call the functions on the instance like commuter.setXXX(variable);

Community
  • 1
  • 1
Jay
  • 636
  • 6
  • 19