简体   繁体   中英

Array Declaration Error: "Stack around the variable '...' was corrupted"

I'm trying to declare my object which is an array, but I seem to be getting this compile error message:

Stack around the variable 'rainFall' was corrupted.

I am able to input what I have in my array, but as soon as I am done with input, the command prompt closes and then gives me the error.

const int num = 12;

class Stats
{
private:
    double stats[num];
public:
    Stats();
    void setValue(double stats[num], int);
};

//separate file

void Stats::setValue(double stats[num], int i)
{
    for (i = 0; i < num; i++)
    {
        cout << "What was the rain fall for month # " << i + 1 << " ?" 
        << endl;
        cin >> stats[i];
    }
    if (stats[i] < 0)
    {
        stats[i] = 0;
    }
}

//separate file

int main()
{
    double rainFall[num];

    Stats rainStats[num];

    rainStats[num].setValue(rainFall, num);

    return 0;
}
void Stats::setValue(double stats[num], int num)
{
    for (int i = 0; i < num; i++)
    {
        cout << "What was the rain fall for month # " << i + 1 << " ?" << endl;
        cin >> stats[i];

        if (stats[i] < 0)
        {
            stats[i] = 0;
        }   
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM