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
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.