Hephaestus
Hephaestus

Reputation: 59

c++ modify multiple arrays through a function

I am relatively new to c++ and I need to load data(a series of integers) from a file into multiple single dimension parallel arrays using a function. The program is to simulate tracking baseball stats, and the file should be opened from the function. I can load the arrays no problem in the main function, but when I setup using a secondary function I end up getting garbage input for all but the first element of the arrays and first element contains what should be in the last used element, unless I open the file in the main function and use a reference to my ifstream and all arrays.

The arrays cannot be global(that would solve the issue), and must be passed to the function. The arrays are set to a size of 20, but the amount of elements used is unknown. I am turning in the project as I have it working now, but I would like insight as to how this would be possible for future projects.

In case I was a little confusing, a summary of the problem I would like help with is how to read file data into multiple arrays using a function and opening the file withing the function. I am using VS2012 on Win7.

Here is snippet the main and prototype working properly:

int loadArrays(int &, int &, int &, int &, int &, ifstream &);

int main()
{
    const int SIZE = 20;
    int playerNum[SIZE], atBats[SIZE], hits[SIZE], runs[SIZE], rbis[SIZE], batAvg[SIZE], numberOfPlayers;

    ifstream statsIn;
    statsIn.open("Batting Stats.txt");
    if (statsIn.fail())
        // this will alert user if there is a problem opening the file
        cout << "Error opening the file\n";

    // load arrays from file
    for(int count = 0; count < SIZE; count++)
    {
        numberOfPlayers = loadArrays(playerNum[count], atBats[count], hits[count], runs[count], rbis[count], statsIn);      
        // stops inputing to arrays once no more data is in the file, loop iterates one extra time to test for garbage input.
        if(playerNum[count] < 1)
            break;
    }
statsIn.close();

And this is the function being called working properly:

/*This function will read the data from the file and store it into the proper arrays, as 
well as collect the number of players based on the total number of calls to this function. 
Function will be called one extra time to test for garbage input into arrays. Return 
value is the number of players plus one due to test, negate issue by decrementing 
return value by one.*/

int loadArrays(int &player, int &bat, int &hit, int &run, int &rbi, ifstream &stats)
{
    static int count = 0;

    stats >> player;
    stats >> bat;
    stats >> hit;
    stats >> run;
    stats >> rbi;
    count++;

    return count - 1;
}

When I move the the code to look like the below I have the issue of not being able to modify all the elements, therefore getting garbage after the first one. I set up the count as a reference thinking that if it was modified in the main program then the elements being passed to the function would auto update since they are using the counter to stay parallel. I think that is where the problem lies, but I couldn't find anything that shows if this is a dead end.

If I use a loop to pass the array elements individually then I will be closing the file and reopening it each time the loop iterates, resetting my cursor position and causing other headaches. Also with the file being open withing the function I wouldn't have the problem of my return value being one higher, but that's not affecting the operation of the function.

Main and prototype:

int loadArrays(int &, int &, int &, int &, int &, int &);

int main()
{
    const int SIZE = 20;
    int playerNum[SIZE], atBats[SIZE], hits[SIZE], runs[SIZE], rbis[SIZE], batAvg[SIZE], numberOfPlayers, count = 0;

    // load arrays from file
    numberOfPlayers = loadArrays(playerNum[count], atBats[count], hits[count], runs[count], rbis[count], count);        

And the function being called:

int loadArrays(int &player, int &bat, int &hit, int &run, int &rbi, int &count)
{   
    ifstream statsIn;
        statsIn.open("Batting Stats.txt");
    if (statsIn.fail())
        // this will alert user if there is a problem opening the file
        cout << "Error opening the file\n";
    while(statsIn >> player)
    {
        statsIn >> bat;
        statsIn >> hit;
        statsIn >> run;
        statsIn >> rbi;
        count++;
    }
    statsIn.close();
    return count;
}

Any and all help is very much appreciated, thank you.

Upvotes: 2

Views: 487

Answers (1)

Mateusz Kacprzak
Mateusz Kacprzak

Reputation: 293

In the first code snippet you're using an external loop to iterate through elements and pass them (by reference) to the function. On the other hand, in the second snippet, you're using a loop inside of function, but passing the same elements - not whole arrays, so you're accessing only single element of each array. Solution (pseudo code):

int loadArrays(int [], int [], int [], int [], int [], int);

int main()
{
    const int SIZE = 20;
    int playerNum[SIZE], atBats[SIZE], hits[SIZE], runs[SIZE], rbis[SIZE], batAvg[SIZE], numberOfPlayers, count = 0;

    // load arrays from file
    numberOfPlayers = loadArrays(playerNum, atBats, hits, runs, rbis, count);
    ...

and the function itself:

int loadArrays(int player[], int bat[], int hit[], int run[], int rbi[], int count)
{   
    ifstream statsIn;
    statsIn.open("Batting Stats.txt");
    if (statsIn.fail())
        // this will alert user if there is a problem opening the file
        cout << "Error opening the file\n";
    while(statsIn >> player)
    {
        statsIn >> bat[count];
        statsIn >> hit[count];
        statsIn >> run[count];
        statsIn >> rbi[count];
        count++;
    }
    statsIn.close();
    return count;
}

Upvotes: 1

Related Questions