DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C (http://www.daniweb.com/forums/forum118.html)
-   -   Help with C code (http://www.daniweb.com/forums/thread153190.html)

gmdune Oct 23rd, 2008 10:48 pm
Help with C code
 
Hi All,

Thanks for all your help and suggestions...I am re-posting my code, with proper formatting for your review. The code runs correctly, but the ending balance, after all the withdrawals, is incorrect and I can't find where I messed up. Any help would be most appreciated:

#include <stdio.h>

int main()
{

    /* Declare Array Variables */
   
    float deposits[50] = {0};
    float withdrawals[50] = {0};
    char  first_name[20] = {0};

    /* Declare Variables */

    int  num_withdrawals, num_deposits, x;
    float current_balance = 0;
    float start_bal;
    float total_deposits;
    float total_withdrawals;
    float balance;
   
    /* Output initial greeting */

    printf("Welcome to the Banking System.\n\n");

    printf("Please enter your first name: ");
    scanf("%s", first_name);

    printf("\nHello, %s.\n\n", first_name);

    /* Get Starting Balance */   
   
    do {
       
        printf("%s, Please enter your current balance in dollars and cents: "); /* Prompt user for current balance. */
        scanf("%f", &start_bal);

        if (start_bal < 0)
            printf("Invalid entry. Starting balance must be at least zero!\n\n");

    } while (start_bal < 0); /* End do-while */
   
    /* end function get starting balance */

    /* Get Number of Withdrawals */

    do {
       
        printf ("\nEnter the number of withdrawals: ");
        scanf ("%i", &num_withdrawals);
        printf ("\n");

        if (num_withdrawals < 0 || num_withdrawals > 50)
            printf ("Error: Number of withdrawals must be between zero and 50, please re-enter!\n\n");
   
    } while (num_withdrawals < 0 || num_withdrawals > 50); /* end do-while trap loop */

    /* end function number of withdrawls */

    /* Get Number of Deposits */

    do {
       
        printf ("Enter the number of deposits: ");
        scanf ("%i",&num_deposits);
        printf ("\n");
       
        if ( num_deposits < 0 || num_deposits > 50)
            printf ("Error: Number of deposits must be between 0 and 50, please re-enter!\n\n");
   
    } while (num_deposits < 0 || num_deposits > 50); /* end do-while trap loop */

    /* Get Each Deposit */

    for (x = 1; x <= num_deposits; x++)
   
    do {
       
        printf ("Enter the amount of deposit #%i: ", x);
        scanf ("%f",&deposits[x]);
       
        if (deposits[x] <= 0)
            printf ("*** Deposit amount must be greater than zero. Please re-enter! ***\n");
   
    } while (deposits[x] <= 0);
   
    total_deposits = total_deposits + deposits[x];
    current_balance = total_deposits + start_bal;   
   
    /* end for loop */
     
    /* Get Each Withdrawal */

    for (x = 1; x <= num_withdrawals; x++)
   
    do {
       
        printf ("\n");
        printf ("Enter the amount of withdrawal #%i: ", x);
        scanf ("%f",&withdrawals[x]);
       
        if (withdrawals[x] > current_balance)
            printf ("***Withdrawal amount exceeds current balance.***\n");
       
        else
            if (withdrawals[x] <= 0)
                printf ("*** Withdrawal amout must be greater than zero. Please re-enter! ***");
       
    } while (withdrawals[x] > current_balance || withdrawals[x] <= 0); /* end do-while loop */
   
    total_withdrawals = total_withdrawals + withdrawals[x];
   
    /* end get each withdrawl */
   
    /* Check Balance */
   
    balance = current_balance - total_withdrawals + total_deposits;
   
    if (balance == 0)
       
        {
       
        printf ("\n *** Balance is now zero. No more withdrawals can be made at this time. ***\n");
       
        num_withdrawals = x;
       
        }
   
     
    /* Calculate and Display Balance */
   
    {
   
    printf ("\n*** Your closing balance is $%.2f, %s*** \n", balance, first_name);
        if (balance >= 5000.00)
       
    printf ("*** Time to invest some money!*** \n\n");
        else if (balance >= 15000.00 && balance <= 49999.99)
       
    printf ("*** Maybe you should consider a CD.*** \n\n");
        else if (balance >= 1000.00 && balance <= 14999.99)
           
    printf ("*** Keep up the good work.*** \n\n");
   
        else
    printf ("*** %s, your balance is getting low!*** \n\n", first_name);
   
    }  /*end-if*/
   
    /* Calculate and display balance*/
   
    printf ("    *** Bank Record ***\n");
   
    printf ("\nStarting Balance:  $%13.2f\n\n", current_balance);
   
    for (x = 1; x <= num_deposits; x++)
       
        {
       
        printf ("Deposit #%i:          %13.2f\n", x, deposits[x]);
       
        }
   
    for (x = 1; x <= num_withdrawals; x++)
       
        {
       
        printf ("\nWithdrawal  #%i:      %13.2f", x, withdrawals[x]);
       
        }
   
        printf ("\n\nEnding Balance is:  $%13.2f\n", balance);
   
    /* end function display bank record */
   
    getchar(); /* Pause output */

    return (0);

}  /* end main */

Ancient Dragon Oct 23rd, 2008 11:05 pm
Re: Help with C code
 
>>.I am re-posting my code
This is your first post -- so how can that be ???

>>The code runs correctly, but the ending balance, after all the withdrawals, is incorrect

Then it doesn't run correctly, now does it??


What compiler are you using? VC++ 2008 Express produces a couple warnings that are really errors. You need to correct them.

1>c:\dvlp\test1\test1\test1.cpp(93) : warning C4700: uninitialized local variable 'total_deposits' used
1>c:\dvlp\test1\test1\test1.cpp(117) : warning C4700: uninitialized local variable 'total_withdrawals' used

Salem Oct 24th, 2008 1:38 am
Re: Help with C code
 
Serial killer
http://cboard.cprogramming.com/showthread.php?t=108441
http://forums.devshed.com/c-programm...ng-565811.html


All times are GMT -4. The time now is 4:41 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC