填充数组(访问冲突写入位置0x00000000)

时间:2013-12-10 02:34:29

标签: c++

当我想要填充days[]时,我的代码开始中断;当我想填补ratio2[]时,它也会中断。我的所有数组都在main()中声明并初始化为MAX,在全局定义中定义为50。我需要在每个索引中存储一个值然后打印它。

void getdata(ifstream &inFile, int id[], int days[], double ratio2[], double estmax_heart[], double ratio[], double avg[])
{
    int i; int j = 0;
    int num;
    double largest = 0;
    int n = 0;

    int max_heart = 0; int age = 0;
    double avg_comm_rate = 0; 
    double maxcomm_rate = 0;
    double exc_rate = 0;
    double sum = 0;


    while (! inFile.eof())
    {       
        inFile >> id[n];
        cout << id[n] << " ";

        inFile >> max_heart;
        cout << max_heart << " ";

        inFile >> age;
        cout << age << " " << endl;

        estmax_heart[n] = 220-age;

        ratio[n] = (max_heart/estmax_heart[n]) * 100;

        days = 0; largest = 0; sum = 0; ratio2 = 0; j = 0;

        for (i = 0; i < 5; i++)
        {   
            inFile >> avg_comm_rate;
            cout << avg_comm_rate << " ";

            inFile >> maxcomm_rate;
            cout << maxcomm_rate << " ";

            inFile >> exc_rate;
            cout << exc_rate << endl;

            if (exc_rate != 0)
            {
                days[i] = j + 1;        
            }

            if(maxcomm_rate > largest)
            {
                largest = maxcomm_rate;
            }

            sum = sum + avg_comm_rate;

            ratio2[i] = (largest / max_heart) * 100;
        }

        n++;    

        calculate(i, days, sum, avg_comm_rate, maxcomm_rate, exc_rate, avg, largest, estmax_heart, ratio, ratio2);

        system("pause");

        //cout << sum;
    }

    inFile.close();

    return;

}

1 个答案:

答案 0 :(得分:6)

这是因为在访问之前将数组指针分配给0

days = 0; largest = 0; sum = 0; ratio2 = 0; j = 0;