# Totaling up numbers in a for loop?

• 11-21-2001
musayume
Totaling up numbers in a for loop?
I'm attempting another simple calculation program... a bank program to be specific. It asks how many deposits does the user want, user inputs x... program asks to input x amount of deposits. Same goes for withdraws.

What I can't figure out is that I want the program to display totals of a deposit. Here's what I have so far
Code:

```printf ("\nPlease enter your beginnnng balance:  \$"); scanf ("%f", &beg_bal); printf ("\nYour beginning balance is:  \$%-10.2f", beg_bal); cur_bal = beg_bal; printf ("\n\nHow many deposits will you be making? "); scanf ("%d", &depamt);   for (index = 1; index <= depamt; index++)   {   printf ("\nPlease enter a deposit: \$");   scanf ("%f", &deposit);   printf ("\nYou have deposited    : \$%6.2f", deposit);   cur_bal+=deposit;   printf ("\nYour current balance is: \$%6.2f\n", cur_bal);   }```
So basically I have that... but after that, I want to be able to display something like:

"You have made x deposits which totals \$x.xx."

I'd do something like:
printf ("You have made %d deposits which totals \$%f", depamt, ...);

But I've no idea what I can use for the "...". I'm guessing I can do some kind of a foruma for the for loop body but I've no idea what to do.

Since this is a for loop, it'd be dumb to include a float variable for each deposit... any way to accomplish this? :) Thanks.
• 11-21-2001
Unregistered
I think if you just created a single new variable (cur_deposits) or something like that, and set it to zero before the loop, then add a statement in the loop like

cur_deposits = cur_deposits + deposit

then when you exited the loop, it would contain the total deposited...?
• 11-21-2001
Salem
printf ("You have made %d deposits which totals \$%f\n", depamt, cur_bal-beg_bal );

Perhaps....
• 11-21-2001