How to pass a file into a function?

51,294

The way you pass an ifstream into the function is perfectly fine.

I suspect that the problem lies in the way you are managing your array of StudentType and its size (numStudents). I would recommend changing your code to use an std::vector instead of a raw array. In general, you should always prefer vectors over arrays unless you have a really good reason to use an array.

vectors can grow to accommodate more data and keep track of their size, so you don't have to.

Also, it's a good idea for functions to return objects rather than modify objects passed through the parameter list.

#include <vector>
using namespace std;

vector<StudentType> ReadStudentData(ifstream& infile) {
    vector<StudentType> students;
    string lastName, firstName;
    int testScore;
    while (infile >> lastName >> firstName >> testScore) {
        if (testScore >= 0 && testScore <= 100) {
            StudentType student;
            student.studentName = lastName + ", " + firstName;
            student.testScore = testScore;
            students.push_back(student);
        }
    }
    return students;
}

// call the function
vector<StudentType> students = ReadStudentData(infile);

// or if you have a C++11 compiler
auto students = ReadStudentData(infile);

// use students.size() to determine how many students were read
Share:
51,294

Related videos on Youtube

Mickey
Author by

Mickey

Updated on April 28, 2020

Comments

  • Mickey
    Mickey almost 4 years

    i am having difficulty understanding how to pass a file into a function.

    i have a file with 20 names and 20 test scores that needs to be read by a function. the function will then assign the names and scores to a structure called student.

    my question is how would i write a function call with the appropriate parameters. ? to make my function read the data in the file. thanks.

    CODE

    // ask user for student file
    cout << "Enter the name of the file for the student data to be read for input" << endl;
    cout << " (Note: The file and path cannot contain spaces)" << endl;
    cout << endl;
    cin >> inFileName;
    inFile.open(inFileName);
    cout << endl;
    
    // FUNCTION CALL how do i set this up properly?
    ReadStudentData(inFile, student, numStudents ); 
    
    void ReadStudentData(ifstream& infile, StudentType student[], int& numStudents)
    {
        int index = 0;
        string lastName, firstName;
        int testScore;
    
        while ((index < numStudents) &&
               (infile >> lastName >> firstName >> testScore))
        {
            if (testScore >= 0 && testScore <= 100)
            {
                student[index].studentName = lastName + ", " + firstName;
                student[index].testScore = testScore;
                index++;
            }
        }
    
        numStudents = index;
    }
    
    • iammilind
      iammilind about 13 years
      what is the problem you are facing with your current code ? I find that, you are passing the parameters properly.
  • Casey
    Casey over 10 years
    This has a homework smell to it; the OP may not have been able to use vectors and was being told to use raw arrays....and the question is 2 years old at this point.

Related