【问题标题】:c++ modify multiple arrays through a functionc++通过一个函数修改多个数组
【发布时间】:2015-08-06 09:33:08
【问题描述】:

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

数组不能是全局的(这将解决问题),并且必须传递给函数。数组的大小设置为 20,但使用的元素数量未知。我正在上交该项目,因为我现在正在运行它,但我想了解未来项目如何实现这一点。

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

这里是 sn-p 的主要和原型工作正常:

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

非常感谢您的任何帮助,谢谢。

【问题讨论】:

    标签: c++ arrays function file


    【解决方案1】:

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

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

    【讨论】:

    • 感谢您的快速响应,最终解决了我的问题,尽管我也在函数中定义了计数。我能够快速解决这个问题,我将提交更新!
    猜你喜欢
    • 1970-01-01
    • 2020-07-25
    • 1970-01-01
    • 1970-01-01
    • 2020-12-04
    • 1970-01-01
    • 1970-01-01
    • 2012-08-14
    • 1970-01-01
    相关资源
    最近更新 更多