I am working on writing a program for my C programming class in which we have to make a basic program that can balance a checkbook. It prompts the user for a starting balance, can add and subtract amounts, and should deduct a $10 fee if the user overdrafts. The code I have so far is giving me some trouble.

#include <stdio.h>
#include <stdlib.h>
 
int main ()
{
    float bal, creditDebit, newBalance ;
 
    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", &newBalance);
    
       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);
    }
 
 
 
    system("pause");
    exit(0);
}

It takes the initial balance and will let me input a credit or a debit but the part where it is supposed to display a new balance after each deposit is where I am getting some trouble.

Recommended Answers

All 5 Replies

It takes the initial balance and will let me input a credit or a debit but the part where it is supposed to display a new balance after each deposit is where I am getting some trouble.

Then you didn't program the thing right. You said you're "getting some trouble", but that's not a description of a problem. It's asking us to figure out what you already know but were too lazy to actually explain.

I'm aware of that. Would you like an output? When it gives the new balance, no matter what you put in, it returns a digit of almost ten numbers, seemingly unrelated.

What is bal equal to, prior to line 28?

What is bal equal to, prior to line 28?

Are you saying I need to declare a value for bal before my while statement?

When you declare a variable

float var; (or int variable; etc.)

var starts off containing garbage (whatever happens to be in memory at that spot, interpreted as a float value, which is why you get these large negative values). Initialize it to 0.0, but you'll need to indicate to the compiler that 0.0 is a float constant and not a double by writing 0.0F

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.