I am getting an error of "syntax error befor "else" " when i enter this code into the compiler:

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

int main ()
{
    float Balance, 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", &Balance);
    
    printf("\n");
    
    printf("Enter deposit (+) or Withdrawl (-): ");
    
    scanf("%f", &creditDebit);
    
    newBalance = Balance - creditDebit;
    
    printf("Current balance: %.2f\n", newBalance);
    
    if ( newBalance < 0)
        printf("***I am sorry, you have bounced this check. $10 will be");
        printf("deducted\n");
        printf("Current balance: %.2f\n", newBalance - 10);
        scanf("%.2f", &newBalance);   
   else if ( newBalance > 0)
         printf("Current balance: %.2f\n", newBalance);
    else if ( newBalance = 0)
         printf("At end of program your final balance is: %.2f\n", 
                      newBalance);
    
    system("pause");
    exit(0);
}

I am using cascaded if statements how my book shows me, am i maybe not understanding it correctly?

can anyone offer any help?

If you have more than one statement within an if/else/else if, you need braces around the block.

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

Thank you John. That solved one problem, however i am on to a new problem with a loop. When my program asks for a debit or a credit a 0 entered by the user will exit from the program. i used a while loop to do this. if i enter 0 it works fine, however if i enter a dollar amount + or - it computes a correct balance and displays it as it should but just hangs there and does not move on through the if statements...I am stumped...can i not use if statements in a while loop, is there a better way to implement what i am trying to do?

My output should look like this:

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

This is the code i am trying to use:

#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);
    
    printf("\n");
    
    printf("Enter deposit (+) or Withdrawl (-): ");
    
    scanf("%f", &creditDebit);
    
    while(creditDebit != 0)
    
{
    
    newBalance += creditDebit;
    
    printf("Current balance: %.2f\n", newBalance);
    scanf(" %.2f", &bal);

    if (bal < 0)
{
        printf("***I am sorry, you have bounced this check. $10 will be"
        "deducted\n");
        printf("Current balance: %.2f\n", newBalance - 10);
        scanf("%.2f", &bal);
        printf("Current balance: %.2f\n", newBalance);
}
    else    
{
        printf("Current balance: %.2f\n", newBalance);
        printf("Enter deposit (+) or Withdrawl (-): ");
        scanf("%f", &creditDebit);
}

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

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

Your program "hangs" here:

scanf(" %.2f", &bal);

Actually, it's not really hanging. It's just waiting for input.

I'd say you're making this more complicated than it needs to be. Use one balance variable. Don't subtract from it until you've verified that the "check" won't bounce.

What input is it waiting for? shouldnt scanf read from the printf line above it. I was using all newBalance before and it was doing the same thing. I am new to this and just generally confused with it sometimes.

What input is it waiting for? shouldnt scanf read from the printf line above it. I was using all newBalance before and it was doing the same thing. I am new to this and just generally confused with it sometimes.

Take John A's advice and just use one balance variable (e.g. bal and get rid of newBalance)

Remove these lines just before your while loop:

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

You're basically getting all your printfs and scanfs screwed up. Try this for your loop:

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);
    }

In your original code snippet, you were printing the current balance and then calling scanf, whereas you need to print your prompt for credit/debit and then call scanf to achieve the output you require.

Thanks YellowSnow, and John! I figured that out before i went to bed last night i just forgot to update the message board. Sometimes i feel like an idiot, but if i already knew it all i guess i wouldnt need to take a class for it....Thanks for your guys help, much appreciation!

Rob Brown,

Can you post your complete working code which will be beneficial for me and to the community ? Thanks.

the code is mostly there in the posts, its not hard to figure out how to fix it. I believe at the top of the message board homework help is only offered to those who show effort....I would hate to give someone complete code for HW and have them not learn it. I have benifited quite alot from posting horrible code and getting answeres to my problems, you might try that people do respond quite quickly.

commented: Well said! +2

Rob Brown,

Can you post your complete working code which will be beneficial for me and to the community ? Thanks.

More likely beneficial for you - so you don't have to lift a finger to do your assignment.
<start-rant>
In my learning days, we weren't even allowed near an editor/compiler until we had put something down on paper (yes paper) outlining the approach we were planning to take to solve a problem. We usually did this with psuedocode or flowcharts (do CS students even know what flowcharts are these days?)

So before laying a finger on the keyboard, get out a piece of paper, grab yourself a sharp pencil and work out how you plan to approach a problem before typing in the code. If you can't do this, then you're in the wrong game mate.
</start-rant>

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.