Hi,

I'm trying to compute a moving average of length 20. I've got a vector of structs that's read into my function and I'm using an iterator to read through as follows:

    int length = a;
    int count = 0;
    double sum = 0;

    while (itr!= priceinput.end()) 

    {   
            sum += itr->Close;
            count ++;
            if (count >= length)
                {
                    itr->MovingAve = (sum / (double) length);
                    sum -= (itr-length)->Close; // this is the problem line
                }

So I'm trying to find out what the right syntax is for the sum-= line. When count gets to 21, I have to subtract the 1st number from the sum, so that's (count-length). I've been googling for the right syntax to do this with the iterator, in this case, itr, but no luck so far.

Any ideas?
Thanks,
TR

Never mind guys, I figured it out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.