Adding values for each iteration of for loop

I have a for loop with 9 iterations and inside that loop I have calculated an int variable using a line of code from an input file. Lets say for the first iteration my value was 20, for the second iteration my value was 7, and for the third iteration my value was 8. I need to output 20 for the first iteration, 27 for the second, and 35 for the third. Is there a way to do this?? I can't seem to figure out how to "extract" those previous values. Thanks for the help.
You can set a total variable to zero. Then for each iteration of the loop, add on the amount you want using compound assignment, if you know what that is:
1
2
total += somenum; // this is the same as
total = total + somenum; // basically adds somenum to total's current value 
this is a variable though... the line of code varies and the numbers aren't going to be the same every time so I can't do that.
Uh, why not?
1
2
3
4
5
for (int x = 0; x < somenum; x++)
{
    num = // your calculations here
    total += num;
}

If num is changing it's not going to add the same number over and over again.
If you're actually calculating to a different variable every time, I don't even see the point of you using a loop. You should put your calculation result into the same variable each time. If you don't like the solution we'll need more information on your homework.
Topic archived. No new replies allowed.