8

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;
}
1
  • 2
    what is the problem you are facing with your current code ? I find that, you are passing the parameters properly. Commented Apr 11, 2011 at 4:25

2 Answers 2

4

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
Sign up to request clarification or add additional context in comments.

1 Comment

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.
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.