Thread: Totaling up numbers in a for loop?

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    11

    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.

  2. #2
    Unregistered
    Guest
    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...?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    printf ("You have made %d deposits which totals $%f\n", depamt, cur_bal-beg_bal );

    Perhaps....

  4. #4
    Mad Mikey
    Guest
    Your problem looks just like an on-going project I have in my "Introduction to C Programming" class. It involves asking for a current balance, amount of monthly payment, and the annual interest rate.

    Every evolution of the project involves changing the way the information is input by the user and crunched by the program. The last one was using one dimensional arrays to store, crunch, and display the numbers received.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing unique numbers to an array
    By yardy in forum C Programming
    Replies: 6
    Last Post: 12-27-2006, 09:15 PM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. Problem with totaling numbers that was spinned by a dice
    By bajanstar in forum C Programming
    Replies: 6
    Last Post: 03-05-2005, 01:37 PM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM