Archey
Archey

Reputation: 1342

Adding in a For Loop

I've written a small c++ program to do a calculation based on a simple algorithm. What I'm trying to do is run the algorithm multiple times and add all the values for print out a total value at the end of the loop.

For refence here is the algorithm:

2^y * 25 * 100^(z/100)

Y would be a value input by the user, Z would also be a value from 1-100.

Here is my For Loop:

    for(int i=0;i<SkillLeft;i++){
        SkillLevel = SkillLevel+0.01;
        float SubTotal = BasePower*25*(pow (100,SkillLevel));
        Total = DerpTotal+SubTotal;

        cout << "Sub: " << SubTotal << endl;
        cout << "Total: " << Total << endl;
    }

When this is ran with the rest of my code it calculates correctly, but instead of adding each subtotal to the total, it basically just multiplies it by 2.

So how can I get it to add each subtotal iteration to a total without "resetting" the varible.

Upvotes: 1

Views: 679

Answers (1)

Luchian Grigore
Luchian Grigore

Reputation: 258568

Just write:

Total += DerpTotal+SubTotal;

instead. You haven't told use what DerpTotal is, so the above might be

Total += SubTotal;

and you had just made a typo, and actually meant

Total = Total+SubTotal;

which would make more sense.

Upvotes: 1

Related Questions