0

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?

Cormac
  • 25
  • 8
  • [`std::getline`](https://en.cppreference.com/w/cpp/io/basic_istream/getline) reads a "line" into a string. It doesn't call functions you pass. My suggestion is that you create an overloaded input `operator>>` function to read from input streams. This function reads the *whole* line into a string, put the string into an `std::istringstream` that you can then extract the data of the line, into *strings*, then call the appropriate functions to set the values of the object. – Some programmer dude Apr 08 '19 at 11:26
  • Okay thanks for your comment. Is it best to have this function in the main or student cpp? I could have it in the student and return a vector of objects to the main. Does this matter at all – Cormac Apr 08 '19 at 13:58
  • @Cormac It doesn't matter except from the point of view of code organization. Since this function is primarily dealing with students it belongs with the other student code, IMHO. – john Apr 08 '19 at 14:10

0 Answers0