0

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.

3
Contributors
5
Replies
8
Views
6 Years
Discussion Span
Last Post by jonsca
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.

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.

0

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.

0

What is bal equal to, prior to line 28?

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

0

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

Edited by jonsca: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.