1

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.

1
  • You really should learn to use a debugger (like e.g. gdb on Linux) and to compile with warnings and debugging info enabled (e.g. g++ -Wall -g on Linux). Familiarity with the debugger is a required skill. Commented Oct 4, 2012 at 5:35

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh gosh! Yeah DerpTotal is a typo. Total += SubTotal worked just fine, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.