Mickey
Mickey

Reputation: 91

How to pass a file into a function?

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

Upvotes: 8

Views: 65708

Answers (2)

YoungLearner
YoungLearner

Reputation: 55

The reference to the file object seems to be fine, but the array of StudentType objects maybe wrong. Try this:

void ReadStudentData(ifstream& infile, 
std::vector<StudentType>& vecStudents, 
int& numStudents)

Upvotes: 0

Ferruccio
Ferruccio

Reputation: 100658

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

Upvotes: 4

Related Questions