mercredi 5 septembre 2018

How to pass a global variable into a function in C++?

I am currently learning to parse a file that contains some data on the student's grades.

std::ifstream f;

int main()
{
    //std::ifstream f;
    parse_store(f, "student_grades.txt"); //this line is throwing the error

    //keep the window from shutting down
    system("pause");

    return 0;
}

The parse_store function parses the txt file, tokenizes each line of data, then sends the tokenized vector (looks like this ["Hello", "World"]) into a class that gets stored in an array of class objects.

for context:

//parses the file returning a vector of student objects 
std::vector<Student> parse_store(std::ifstream file, std::string file_name) {

    //array of objects
    std::vector<Student> student_info;

    //create string to store the raw lines
    std::string line;

    // the delimiter
    std::string delimiter = " ";

    //open te file
    file.open(file_name);

    //create vector to hold the tokenized list
    std::vector<std::string> tokenized;

    //index
    int index = 0;
    while (file) { 

        //keep track of the index
        index++;

        //create a vector to hold each student's grades (will hold the objects)
        std::vector<std::vector<std::string>> grades = {};

        //read a line from file
        std::getline(file, line);

        //delimit the line and send it to the constructor
        tokenized = delimitMain(line, delimiter);
        student_info.push_back(Student(tokenized, index));
    }

    file.close();

    return student_info;
}

Why is the line above throwing the error? Is there any problem with the way I am putting the file object into vector and then returning it?

Aucun commentaire:

Enregistrer un commentaire