2017-04-01 4 views
0

저는 C++ 초보자입니다. 아래에 표시된 내 기본 코드는 csv 파일을 읽고 한 줄씩 데이터를 인쇄 할 수 있습니다. 그러나, 나는 이러한 모든 데이터를 정의 된 통근자 클래스 변수에 할당하는 방법을 알 수 없습니다. 나는 많은 온라인 튜토리얼을 시도했지만 항상 디버깅 방법을 모르는 오류를 보여줄 것이다. 누구든지 나에게 손이나 힌트를 줄 수 있을까? 고맙습니다.CCSV 파일에서 C++을 읽고 클래스 멤버에게 데이터를 할당합니다.

샘플 데이터 : 당신은 몇 단계를 가지고 수업에 그것을 추가 할 수 있어야하므로

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

"오류 표시"는 무엇을 의미합니까? 우리는 마음을 읽는 독자가 아니므로 오류를 알려주십시오. 건물을 지을 때 얻을 수 있습니까? 달릴 때? 프로그램이 충돌합니까? 예상치 못한 결과를 표시 하시겠습니까? [좋은 질문을하는 법을 읽으십시오.] (http://stackoverflow.com/help/how-to-ask)로 시간을내어 질문 *을 편집하여 자세한 내용을 포함 시키십시오. –

답변

0

당신은, 당신의 str 변수의 데이터 라인을 가지고있다.

먼저 split the string into components을 알아야합니다.

다음 단계는 문자열의 각 구성 요소를 필요한 데이터 유형으로 변환하는 것입니다. atoi은 문자열을 int로 변환하는 데 좋은 함수입니다.

클래스의 인스턴스도 필요합니다. 예 : Commuter commuter; 그런 다음 인스턴스의 함수를 호출 할 수 있습니다. commuter.setXXX(variable);

+0

모든 힌트를 가져 주셔서 감사합니다. 나는 그들에 대해 검색하고 내 기본 코드로 테스트하려고합니다. –