I'm having issues with this checkbook program. The probelm is when I deposit money into the balance it still shows me the old overdrafted balance. I am thinking that it has to do with my:

bal += CreditDebit;

Here is my code so far:

#include <stdio.h>
#include <stdlib.h>

int main ()
{
    float bal, CreditDebit;
    
    printf("This is your checkbook balancing utility.\n");
    
    printf("You will enter your current balance followed by\n");
    
    printf("checkbook entries. Use + and - to indicate deposits\n");
    
    printf("and withdrawals. Signal the end of processing by\n");
    
    printf("entering a '0'\n");
    
    printf(">>>Please enter your initial balance: ");
    
    scanf("%f", &bal);
    
    
    while (CreditDebit != 0) {

        printf("\n");
        
        printf("Enter deposit (+) or Withdrawl (-): ");
        
        scanf("%f", &CreditDebit);
        
        bal += CreditDebit;

       
        if (bal < 0) {
            printf("***I am sorry, you have bounced this check. $10 will be deducted\n");   
            
            bal -= 10;
            }
            printf("Current balance: %.2f\n", bal);

}
   printf("\nAt end of program your final balance is: %.2f\n",bal);

         
    system("pause");
    exit(0);
}

Any help would be appreciated.

Edited 6 Years Ago by WaltP: Fixed CODE Tags -- please review how they work.

Well this is how my ouput should look like (User input in bold):

This is your checkbook balancing utility.
You will enter your current balance followed by
checkbook entries. Use + and - to indicate deposits
and withdrawals. Signal the end of processing by
entering '0'
>>> Please enter your initial balance: 100
Enter deposit (+) or withdrawal (-) : -55
Current balance: 45
Enter deposit (+) or withdrawal (-) : -50
** I am sorry, you bounced this check. $10 will be deducted.
Current balance: -15
Enter deposit (+) or withdrawal (-) : +10
Current balance:-5
Enter deposit (+) or withdrawal (-) : 0
At end of program your final balance is: -5

....it's works fine until I deposit a postive 10 dollers and it still shows a -15 balance...also after the input '0' (to end the while loop) it shows a new negative balance of -25.00....so it added my 10 doller deposit.

Edited 6 Years Ago by YuriR: n/a

Which line of your post answers my question?

Also, why is it people will tell us what it should do but won't show us what it does do? Why not post your output and show us exactly what happens rather than a 'verbal' description?

Edited 6 Years Ago by WaltP: n/a

The initial value of CreditDebit is 100

This is how my output looks like:

This is your checkbook balancing utility.
You will enter your current balance followed by
checkbook entries. Use + and - to indicate deposits
and withdrawals. Signal the end of processing by
entering a '0'
>>>Please enter your initial balance: 100

Enter deposit (+) or Withdrawl (-): -55
Current balance: 45.00

Enter deposit (+) or Withdrawl (-): -50
***I am sorry, you have bounced this check. $10 will be deducted
Current balance: -15.00

Enter deposit (+) or Withdrawl (-): +10
***I am sorry, you have bounced this check. $10 will be deducted
Current balance: -15.00

Enter deposit (+) or Withdrawl (-): 0
***I am sorry, you have bounced this check. $10 will be deducted
Current balance: -25.00

At end of program your final balance is: -25.00
Press any key to continue . . .

Sorry I'm still new at this and I have never taken programing until now. I am trying to learn this stuff.

The initial value of CreditDebit is 100

Really? Can you point to the line before the while statement that sets the variable to 100?

This is how my output looks like:

This is your checkbook balancing utility.
You will enter your current balance followed by
checkbook entries. Use + and - to indicate deposits
and withdrawals. Signal the end of processing by
entering a '0'
>>>Please enter your initial balance: 100

Enter deposit (+) or Withdrawl (-): -55
Current balance: 45.00

Enter deposit (+) or Withdrawl (-): -50
***I am sorry, you have bounced this check. $10 will be deducted
Current balance: -15.00

Enter deposit (+) or Withdrawl (-): +10
***I am sorry, you have bounced this check. $10 will be deducted
Current balance: -15.00

This is doing exactly what your code says to do. You add 10 to -15, balance becomes -5. Since the balance is < 0 you deduct 10 making the balance -15. Note the message that says you bounced a check?

Enter deposit (+) or Withdrawl (-): 0
***I am sorry, you have bounced this check. $10 will be deducted
Current balance: -25.00

You entered 0. You processed the 0 as a transaction. You might want to change the place where you ask for the amount, or figure out how to prevent a 0 from processing as a transaction.

Hmm I see....The CreditDebit = 0 ...my input is 100. CreditDebit = my withdrawal or deposit

I have to change the 0 value from being used as a deposit or withdraw...OK I think I see what I have to do...thanks!

Edited 6 Years Ago by YuriR: n/a

You are completely missing my point on the variable CreditDebit

#include <stdio.h>
#include <stdlib.h>

int main ()
{
    float bal, CreditDebit;  // here you define the variables
    printf("This is your checkbook balancing utility.\n");
    
    //... more printf's
    
    printf(">>>Please enter your initial balance: ");
    
    scanf("%f", &bal);
    
    // What is the value of CreditDebit here right NOW?  What are you comparing?
    while (CreditDebit != 0) {

Since you have not given CreditDebit a value, what are its possible values?

Oh and my initial value of CreditDebit is floating...there is no value until I give it one...right?? Which means there is an infinite amount of values it can be, until I enter 0 and then the While loop is ended.

Edited 6 Years Ago by YuriR: n/a

Oh and my initial value of CreditDebit is floating...there is no value until I give it one...right?? Which means there is an infinite amount of values it can be, until I enter 0 and then the While loop is ended.

Right (mostly). So what are you comparing when it has no value? Or in fact an unknown value. That's bad programming. You should always have a value when you use a variable. Set it to something before the while

This article has been dead for over six months. Start a new discussion instead.