c ++通过函数修改多个数组

时间:2015-05-24 23:01:17

标签: c++ arrays function file

我对c ++比较陌生,我需要使用函数将数据(一系列整数)从一个文件加载到多个单维并行数组中。该程序是模拟跟踪棒球统计数据,应该从该功能打开该文件。我可以在main函数中加载数组没有问题,但是当我使用辅助函数进行设置时,我最终得到除了数组的第一个元素以外的所有元素的垃圾输入,并且第一个元素包含应该在最后使用的元素中的内容,除非我在main函数中打开文件,并使用对ifstream和所有数组的引用。

数组不能是全局的(可以解决问题),必须传递给函数。数组的大小设置为20,但使用的元素数量未知。我正在进行该项目,因为我现在已经开始工作了,但我希望能够深入了解未来项目的可行性。

如果我有点困惑,我想要帮助的问题的摘要是如何使用函数将文件数据读入多个数组并使用该函数打开文件。我在Win7上使用VS2012。

以下是主要原型和原型正常工作的片段:

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();

这是被称为正常工作的函数:

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

当我将代码移动到如下所示时,我遇到了无法修改所有元素的问题,因此在第一个元素之后得到了垃圾。 我将计数设置为参考,认为如果它在主程序中被修改,那么传递给函数的元素将自动更新,因为它们使用计数器保持并行。我认为这就是问题所在,但我无法找到任何可以证明这是不是死路一条的东西。

如果我使用一个循环来单独传递数组元素,那么我将关闭文件并在每次循环迭代时重新打开它,重置我的光标位置并引起其他麻烦。此外,如果文件正在打开并且该函数没有问题,那么我的返回值就会高一些,但这不会影响函数的运行。

主要和原型:

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

正在调用的函数:

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

非常感谢任何和所有的帮助,谢谢。

1 个答案:

答案 0 :(得分:1)

在第一个代码片段中,您使用外部循环来迭代元素并将它们(通过引用)传递给函数。 另一方面,在第二个片段中,您正在使用函数内部的循环,但传递相同的元素 - 而不是整个数组,因此您只访问每个数组的单个元素。解决方案(伪代码):

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);
    ...

和函数本身:

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