I created a post here last week but it was for a struct.
Looking to add comma delimited values in a text file to members of a struct
I have a text file here with two values, a name and a score. I have a student class that has 4 members which are seen below.
I am looking to add the values in the text file to the corresponding members in the class separating by comma.
First five rows of the students.txt file;
Nubia,Dufrene,70
Louisa,Trippe,49
Aline,Deniz,34
Shery,Munk,63
Angila,Ping,89
My current code;
studentType.h
class q1studentType
{
//Private Memebrs
std::string studentFName;
std::string studentLName;
std::string testScore;
char grade;
public:
//Constructor
q1studentType(std::string studentFName,std::string studentLName, std::string testScore);
//Get & Set
std::string const getStudentFName() { return studentFName; }
std::string const getStudentLName() { return studentLName; }
std::string getTestScore() const { return testScore; }
void setStudentFName(std::string fN) { q1studentType::studentFName = fN; }
void setStudentLName(std::string lN) { q1studentType::studentLName = lN; }
void setTestScore(std::string ts) { q1studentType::testScore = ts; }
//Member Functions
void printStudent();
int stringToInt(std::string convertMe);
//std::vector<q1studentType> initStudents();
~q1studentType();
};
studentType.cpp
q1studentType::q1studentType(std::string sFN, std::string sLN, std::string ts)
{
studentFName = sFN;
studentLName = sLN;
testScore = ts;
stringToInt(testScore);
//assignGrade();
}
main.cpp
void initstudents() {
std::vector<q1studentType> students;
std::ifstream inFile("students.txt");
for (q1studentType i;
getline(inFile, i.setStudentFName, ',')
&& getline(inFile, i.setStudentLName, ',')
&& getline(inFile, i.setTestScore)
; )
{
studentVector.push_back(i);
}
}
I think the function in the main does not work because getline doesnt accept the setMethod as a param. I cant use the class members in the for loop eg. i.studentFName because they are private and only accessible inside the class.
What is the best approach? Should I have the initstudents in the student.cpp file or the main?