incorrect output

This is a discussion on incorrect output within the C Programming forums, part of the General Programming Boards category; Hi guys, I'm fairly new to C as well as programming in general, this is a basic banking program I ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    15

    incorrect output

    Hi guys, I'm fairly new to C as well as programming in general, this is a basic banking program I am working on. It prompts the user to enter name, withdrawal, & deposit amounts then calculates all and gives us the final balance. Well unfortunately its not quite working as it should its basically not calculating the amounts correctly. I have a feeling its due to my loop structure. I just cant figure out what is wrong in there. So any input on this would be helpful.



    Code:
    #include <stdio.h>
    
    int main (void)
    
    {
        int     deposit_process, withDrl, x;
        float   deposit[50], withDrawal[50];
        float   baLance, Sb, Total;
        char    Name[20];
    
    
        printf ("Welcome to the Sears Banking System\n\n");
        printf ("\nPlease enter your first name: ", Name);
        scanf  ("%s", Name);
        fflush (stdin);
    
        /*----------------------------------*/
        /*Prompt user for balance-----------*/
    
        do
        {
        printf ("\nHello %s\n\n", Name);
        printf ("%s, please enter your current balance in dollars and cents: ", Name);
        scanf  ("%f", &Sb);
        fflush (stdin);
    
            if (Sb <= 0)
               printf ("Error: beginning balance must be at least zero, please re-enter.\n");
    
         }  while (Sb <= 0);
    
         /*--------------------------------*/
         /*prompt user for # of withdrawals*/
    
         do
         {
               printf ("\nEnter the number of withdrawals: ");
               scanf  ("%i", &withDrl);
               fflush (stdin);
    
             if (withDrl <= 0)
               printf ("Error: please re-enter.\n");
    
    
         } while (withDrl <= 0);
    
         /*----------------------------------------*/
         /*Prompt user for # of deposits-------------------*/
    
    
    
    
         do
         {
             printf ("\nEnter the number of deposits: ");
             scanf ("%i", &deposit_process);
             fflush (stdin);
    
             if (deposit_process <= 0)
                 printf ("Error: Number of deposits must be at least zero, please re-enter\n");
    
    
         }while (deposit_process <= 0);
    
         /*---------------------------------*/
         /*-depostit loop-------------------*/
    
    
    
         for (x = 1; x <= deposit_process; x++){
    
    
             do
             {
                 printf ("\nEnter the amount of deposit #%i: ", x);
                 scanf ("%f",&deposit[x]);
                 fflush (stdin);
    
                 if (deposit[x] > 0)
    
                 deposit[x] = deposit[x] + baLance;
    
                 else
    
                 printf("***Deposit amount must be greater than zero. Please re-enter!***\n");
    
             }while (deposit_process <= 0);
    
    
    
                baLance = Sb + deposit[x];
    
    
    
         }
         //*-------------------------*//
         //*--withdrawal loop---*//
    
    
    
         for (x = 1; x <= withDrl; x++)
         {
             do
             {
                 printf("\nEnter the amount of withdrawal #%i: ", x);
                 scanf ("%f", &withDrawal[x]);
                 fflush(stdin);
    
                 if (withDrawal[x] <= 0)
                 printf ("***Error: Number of withdrawals must be greater than 0.***");
    
    
                 if (withDrawal[x] <= baLance)
                      baLance = baLance - withDrawal[x];
    
                 else
                 printf ("***Withdrawal amount exceeds current balance, please re-enter.***\n");
    
    
    
    
    
             }while (withDrawal[x] <= 0);
    
                 Total = baLance - withDrawal[x];
    
    
    
    
         }        /*----------------------------------*/
                 /*------closing balance output--------*/
    
    
                     printf("\n***The closing balance %s is $%f***\n", Name, Total);
    
                     if (Total >= 50000)
                     printf("\n***%s it is time to invest some money***", Name);
    
                     if (Total >= 15000)
                     printf("\n***%s maybe you should consider a CD.***\n", Name);
    
                     else if (Total >= 1000)
                     printf ("\n***%s keep up the good work!***", Name);
    
                     else
                     printf ("\n***%s your balance is getting low!***\n", Name);
    
                    /*------------------------------------------------------------*/
                   /*---------------Bank printout--------------------------------*/
    
    
                     printf ("\n              ***Bank Record***\n");
                     printf ("\nStarting Balance: $                  %f\n", Sb);
    
                     for (x = 1; x <= deposit[x]; x++ )
                     printf ("\nDeposit #%i:                          %f\n", x, deposit[x]);
    
                     for (x = 1; x <= withDrawal[x]; x++)
                     printf ("\nWithdrawal #%i:                       %f\n", x, withDrawal[x]);
    
                     printf ("\nEnding Balance:$                       %f\n", Total);
    
    
    
    
    
    
    
    
    
         return 0;
    
        }/*----end of program------*/

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by Alint808 View Post
    Hi guys, I'm fairly new to C as well as programming in general, this is a basic banking program I am working on. It prompts the user to enter name, withdrawal, & deposit amounts then calculates all and gives us the final balance. Well unfortunately its not quite working as it should its basically not calculating the amounts correctly. I have a feeling its due to my loop structure. I just cant figure out what is wrong in there. So any input on this would be helpful.
    How is it not calculating the amounts correctly?

    Additionally, read Why fflush(stdin) is wrong.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Also do you ever expect to have withdrawals or deposits be less that 0? Your error check says no so why are you using %i? These statements should be scanf("%d"...);

    Read this.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You might as well make those ints unsigned, and then just check to make sure they are entering numbers less than the size of your array.
    Code:
        for (x = 1; x <= withDrl; x++)
         {
             do
             {
                 printf("\nEnter the amount of withdrawal #%i: ", x);
                 scanf ("%f", &withDrawal[x]);
                 fflush(stdin);
    
                 if (withDrawal[x] <= 0)
    Arrays start at 0 and go through SIZE-1. So you only get to access array[ 0 ] ... array[ 49 ], not array[ 1 ] ... array[ 50 ].


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You do realize that you could improve this profoundly and simplify your code (to about 20 lines) at the same time...

    Ask for the initial balance, then...

    Enter transaction (+ for deposit) (- for withdrawl) : -32.35

    Now you can process any number of deposits and withdrawls in any order and calculate the new balance on the fly, no arrays or batch processing to worry about... and you can do all your banking with simple addition.

  6. #6
    Registered User
    Join Date
    Jul 2011
    Posts
    15
    Thanks guys, that shed some light on things for me, but what I really to know is what is up with my loop structure the bottom 2 for loops and the middle trap ones, something is wrong with them that is causing the program to add an additional loop after the data has been entered. If you run the program ask for 3 withdrawals and 3 deposits. You enter 1st your three withdrawals then you enter your three deposits on the output side it does not seem to add the start balance & it adds deposit 1, then add deposit 2 with an extra deposit of same amount, then add deposit 3. On the withdrawal side it works a little better but after the 3rd amount it just adds a random float as an extra withdrawal.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Since you have two lines that are adding your deposit to the total:
    Code:
    deposit[x] = deposit[x] + baLance;
    Code:
    baLance = Sb + deposit[x];
    why are you surprised that your deposits get added twice?

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    On a more serious note, why do you have a capital letter in the middle of the word withdrawal and balance?


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Jul 2011
    Posts
    15

    Wink Woops

    What I was trying to do was have a running balance and a starting balance, but I guess thats not quite working out as planned. Ill tinker with it some more....And as far as the L goes I was trying to liven up the variable .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. RPN incorrect output
    By csharp100 in forum C Programming
    Replies: 5
    Last Post: 10-14-2010, 01:36 PM
  2. incorrect output
    By linuxman in forum C Programming
    Replies: 4
    Last Post: 01-03-2004, 08:01 AM
  3. String output, sometimes incorrect
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 03-14-2002, 08:46 AM
  4. Incorrect Output
    By Nutshell in forum C Programming
    Replies: 2
    Last Post: 01-07-2002, 09:11 AM
  5. incorrect output
    By runtojesus in forum C++ Programming
    Replies: 3
    Last Post: 11-05-2001, 04:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21