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