Thread: Cannot figure out error

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    20

    Cannot figure out error

    I am getting these two errors at the end of my program and i cannot figure out why. Any help would be appreciated, thanks. I can add the full program if it will help.

    [Error] C:\c:169: parse error before `else'

    [Error] c:198: parse error at end of input

    Code:
     /* Message displayed depends on the end balance */
     
    
     if (balance > 0);
     {
     
     printf ("*** The account %s has a credit balance ***\n\n", ledger_name);
    
      
     if (balance == 0);
    
     printf ("*** The account %s is now exhausted of funds ***\n\n", ledger_name);
    
     
     else (balance < 0);
    
     
     printf ("*** The account %s is now over the allotted budget ***\n\n", ledger_name);
     } 
    
     
     /* Display of ledger record */
    
    
     printf ("*** Ledger Record ***\n\n");
     printf ("%s", ledger_name);
     printf ("\nStarting Balance: %.2f\n\n", current_balance);
    
     
     for (x = 1; x <= number_of_credits; x++)
     {
     printf ("Credit #%i: %.2f\n", x, credits[x]);
     }
     for (x = 1; x <= number_of_debits; x++)
     {
     printf ("\nDebit #%i: %.2f\n", x, debits[x]);
     }
     {
     printf ("\nEnding Balance: $ %.2f\n", balance);
     {
      
      
     return 0;
     
     }
     } 
    
     
    /* end main*/

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Code:
    else (balance < 0);
    should just be
    else<--- with out the condition.
    Code:
    {
     printf ("\nEnding Balance: $ %.2f\n", balance);
     {
    check the braces.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    20
    ok now i am getting

    [Error] C:199: parse error at end of input

    Code:
    /* Message displayed depends on the end balance */
     
    
     if (balance > 0)
     {
     
     printf ("*** The account %s has a credit balance ***\n\n", ledger_name);
    
      
     if (balance == 0)
    
     printf ("*** The account %s is now exhausted of funds ***\n\n", ledger_name);
    
     
     else 
    
     
     printf ("*** The account %s is now over the allotted budget ***\n\n", ledger_name);
     } 
    
     
     /* Display of ledger record */
    
    
     printf ("*** Ledger Record ***\n\n");
     printf ("%s", ledger_name);
     printf ("\nStarting Balance: %.2f\n\n", current_balance);
    
     
     for (x = 1; x <= number_of_credits; x++)
     {
     printf ("Credit #%i: %.2f\n", x, credits[x]);
     }
     for (x = 1; x <= number_of_debits; x++)
     {
     printf ("\nDebit #%i: %.2f\n", x, debits[x]);
     }
     {
     printf ("\nEnding Balance: $ %.2f\n", balance);
     {
      
      
     return 0;
     
     }
     } 
    
     
    /* end main*/

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    You really need to study C syntax.
    You have extra braces that are not supposed to be there. Line 5, 19, 38, 40, 45.

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    20
    i see what you are saying now thank you

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    20
    Here is my full program. for some reason it keep giving me the credit must be greater than 0 error that i put in. i have it set for less than or equal to zero. i enter a positive number and it gives me the error. can anyone tell me what i am doing wrong. thank you.



    Code:
    
    #include <stdio.h>
    
     int main(void)
    {
    
     /* Declaration of variables. */
    
     int x, number_of_credits, number_of_debits;
     float credits[25], debits[25];
     float current_balance, balance;
     float total_credits = 0, total_debits = 0;
     char ledger_name[20];
    
     /* Display of initial greeting */
    
     printf ("Welcome to Mike's Ledger System\n\n");
    
     /* Prompt for ledger account name. */
    
     printf ("Please enter the ledger account name:");
     scanf ("%s", ledger_name);
     fflush(stdin);
    
     /* Explanation of program */
    
     printf ("This program will allow you to enter credits and debits to the %s account.\n\n", ledger_name);
    
     /* Prompt to enter current balance. (current_balance >= 0) */
    
     do
      {
    
     printf ("Please enter the Trip Expenses current balance in dollars and cents:");
     scanf ("%f", &current_balance);
     fflush(stdin);
    
     /* Display error if current balance is less than 0 dollars. */
    
     if (current_balance < 0)
      printf ("Error: Beginning balance must be at least zero, please re-enter!\n\n");
     } 
     while (current_balance < 0); 
    
     
     /* End loop if current balance is less than 0 dollars. */
    
     
      current_balance = balance;
    
     /* Prompt for number of debits and credits to be posted */
    
     do
     {
    
     printf ("\nEnter the number of debits to post: ");
     scanf ("%i", &number_of_debits);
     fflush(stdin);
    
     if (number_of_debits <= 0)
      printf ("ERROR: Please enter a number greater than zero!\n\n");
     if (number_of_debits > 25)
      printf ("ERROR: Please enter a number no more than 25!\n\n");
     } 
     while (number_of_debits < 0 && number_of_debits > 25);
     do
     {
    
      printf ("\nEnter the number of credits to post: ");
      scanf ("%i", &number_of_credits);
      fflush(stdin);
    
     if (number_of_credits < 0)
     
      printf ("ERROR: Please enter a number greater than zero!\n\n");
     if (number_of_credits >25)
      printf ("ERROR: Please enter a number no more than 25!\n\n");
     } 
     while (number_of_credits < 0 || number_of_credits > 25);
    
     /* for loop starts which will add the credit amounts entered */
    
     for (x = 1; x <= number_of_credits; ++x)
     {
     
    
     /* Prompt for credit amount */
    
     
     do
     {
    
      printf ("Enter the amount of credit #%i: ", x);
      scanf ("%f", &credits[x]);
      fflush(stdin);
    
     
     if (credits[x] <= 0);
      printf ("***Credit amount must be greater than zero. Please re-enter!***\n");
     }
     while (credits[x] <= 0);
    
     /* new value to the total credit will be created by adding the credits */
    
     
     total_credits = total_credits + credits[x];
     }
     
    
     /*end for loop */
    
    
     /* for loop starts to enter and add up debit amounts */
    
     for (x = 1; x <= number_of_debits; ++x)
     {
      
    
     /* Prompt for debit amount */
    
     
     do
     {
    
      printf ("Enter the amount of debit #%i: ", x);
      scanf ("%f", &debits[x]);
      fflush(stdin);
    
    
     /* this loops displays a message if debit amount is greater than the balance*/
    
     
     if (debits[x] > current_balance);
     printf ("***Debit amount exceeds current balance!***\n");
     if (debits[x] <= 0);
     printf ("***Debit amount must be greater than zero! Please re-enter.***\n");
     } 
     
    
     /* loop ends when amount is greater than 0 */
    
     
     while (debits[x] > current_balance || debits[x] <= 0);
    
     /* new value to the total debit by adding the amount entered */
    
     
     total_debits = total_debits + debits[x];
    
    /* this will calculate the balance and ensures the debit is less than the balance*/
    
     
     balance = current_balance - total_debits + total_credits;
     if (balance == 0);
     {
      
    
     /* if balance reaches 0, a message will be displayed and no more debits allowed */
    
     
     printf ("\n *** Balance is now zero. No more debit transactions can be made at this time. ***\n");
     
     number_of_debits = x;
     break;
     
     } 
     
    
     /* end the loop */
     
    
     }
    
     /*Calculate and display ledger balance */
    
     printf ("***The %s balance is $%.2f***\n", ledger_name, balance);
    
     /* Message displayed depends on the end balance */
    
     
     if (balance > 0)
      
      printf ("*** The account %s has a credit balance ***\n\n", ledger_name);
      
     if (balance == 0)
     printf ("*** The account %s is now exhausted of funds ***\n\n", ledger_name);
     
     else
     printf ("*** The account %s is now over the allotted budget ***\n\n", ledger_name);
      
     
    
     /* Display of ledger record */
    
     printf ("*** Ledger Record ***\n\n");
     printf ("%s", ledger_name);
     printf ("\nStarting Balance: %.2f\n\n", current_balance);
    
     for (x = 1; x <= number_of_credits; x++)
     {
     printf ("Credit #%i: %.2f\n", x, credits[x]);
     }
     for (x = 1; x <= number_of_debits; x++)
     {
     printf ("\nDebit #%i: %.2f\n", x, debits[x]);
     }
      printf ("\nEnding Balance: $ %.2f\n", balance); 
      
      
      
     return 0;
     
     }
     
     
    //end main

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Indent your code so it's easier [possible] to follow.
    And don't use fflush(stdin).
    FAQ > Why fflush(stdin) is wrong - Cprogramming.com

  8. #8
    Registered User
    Join Date
    Oct 2012
    Posts
    20
    I am using C-free 4.0 is there a way to copymy code exactly how it is over to the forum?

  9. #9
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Some of your if statements are not going to execute, because of the semi-column at the end. It's really obvious in your first post and still there in your last post.
    Code:
    if (debits[x] > current_balance);
    printf ("***Debit amount exceeds current balance!***\n");
    Try to avoid this - FAQ > Why fflush(stdin) is wrong - Cprogramming.com
    Code:
    fflush(stdin);
    And yes, your indentation is horrible - The IDE "Code::Blocks" copies perfectly and is free. It's the program that this website recommends for C/C++ programming.
    Fact - Beethoven wrote his first symphony in C

  10. #10
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Sorry, I didn't see your link already given, Matticus.

    Oh well.
    Fact - Beethoven wrote his first symphony in C

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    No problem, Click_here. It's a good way to drive the lesson home.

    @OP: See if there's a setting in your IDE to use spaces for tabs. If your code still comes out lacking indentation, then take a few minutes to manually edit it. There are probably better ways, but that's all I can offer.

  12. #12
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    See if there's a setting in your IDE to use spaces for tabs
    Or better yet, whack together a little program that opens a text file and replaces '\t' with " "

    Every time you want to put code up, copy and paste it to a text file, save and close, run the program, and the open and copy to this.
    Fact - Beethoven wrote his first symphony in C

  13. #13
    Registered User
    Join Date
    Oct 2012
    Posts
    20
    Code:
    
    #include <stdio.h>
    int main(void)
    {
    
        /* Declaration of variables. */
    
        int x, number_of_credits, number_of_debits;
        float credits[25], debits[25];
        float current_balance, balance;
        float total_credits = 0, total_debits = 0;
        char ledger_name[20];
    
        /* Display of initial greeting */
    
            printf ("Welcome to Nick's Ledger System\n\n");
    
        /* Prompt for ledger account name. */
    
            printf ("Please enter the ledger account name:");
            scanf ("%s", ledger_name);
            fflush(stdin);
    
        /* Explanation of program */
    
            printf ("This program will allow you to enter credits and debits to the %s account.\n\n", ledger_name);
    
        /* Prompt to enter current balance. (current_balance >= 0) */
    
        do
        {
            printf ("Please enter the Trip Expenses current balance in dollars and cents:");
            scanf ("%f", &current_balance);
            fflush(stdin);
            /* Display error if current balance is less than 0 dollars. */
        if (current_balance < 0)
            printf ("Error: Beginning balance must be at least zero, please re-enter!\n\n");
        }
        while (current_balance < 0);
    
        /* End loop if current balance is less than 0 dollars. */
    
        current_balance = balance;
    
        /* Prompt for number of debits and credits to be posted */
    
        do
        {
            printf ("\nEnter the number of debits to post: ");
            scanf ("%i", &number_of_debits);
            fflush(stdin);
        if (number_of_debits < 0)
            printf ("ERROR: Please enter a number greater than zero!\n\n");
        if (number_of_debits > 25)
            printf ("ERROR: Please enter a number no more than 25!\n\n");
        }
        while (number_of_debits < 0 && number_of_debits > 25);
        do
        {
            printf ("\nEnter the number of credits to post: ");
            scanf ("%i", &number_of_credits);
            fflush(stdin);
        if (number_of_credits < 0)
            printf ("ERROR: Please enter a number greater than zero!\n\n");
        if (number_of_credits >25)
            printf ("ERROR: Please enter a number no more than 25!\n\n");
        }
        while (number_of_credits < 0 || number_of_credits > 25);
        /* for loop starts which will add the credit amounts entered */
        for (x = 1; x <= number_of_credits; ++x)
        {
    
        /* Prompt for credit amount */
    
        do
        {
            printf ("Enter the amount of credit #%i: ", x);
            scanf ("%d", &credits[x]);
    
        if (credits[x] <= 0)
            printf ("***Credit amount must be greater than zero. Please re-enter!***\n");
        }
        while (credits[x] > 0);
    
        /* new value to the total credit will be created by adding the credits */
    
            total_credits = total_credits + credits[x];
        }
    
        /*end for loop */
    
        /* for loop starts to enter and add up debit amounts */
    
        for (x = 1; x <= number_of_debits; ++x)
        {
    
        /* Prompt for debit amount */
    
        do
        {
            printf ("Enter the amount of debit #%i: ", x);
            scanf ("%f", &debits[x]);
            fflush(stdin);
    
        /* this loops displays a message if debit amount is greater than the balance*/
    
        if (debits[x] > current_balance)
            printf ("***Debit amount exceeds current balance!***\n");
        if (debits[x] <= 0)
            printf ("***Debit amount must be greater than zero! Please re-enter.***\n");
        }
    
        /* loop ends when amount is greater than 0 */
    
        while (debits[x] > current_balance || debits[x] <= 0);
    
        /* new value to the total debit by adding the amount entered */
    
            total_debits = total_debits + debits[x];
    
        /* this will calculate the balance and ensures the debit is less than the balance*/
    
            balance = current_balance - total_debits + total_credits;
        if (balance == 0)
        {
    
        /* if balance reaches 0, a message will be displayed and no more debits allowed */
    
            printf ("\n *** Balance is now zero. No more debit transactions can be made at this time. ***\n");
            number_of_debits = x;
        break;
        }
    
        /* end the loop */
    
        }
    
        /*Calculate and display ledger balance */
    
        printf ("***The %s balance is $%.2f***\n", ledger_name, balance);
    
        /* Message displayed depends on the end balance */
    
        if (balance > 0)
            printf ("*** The account %s has a credit balance ***\n\n", ledger_name);
    
        if (balance == 0)
            printf ("*** The account %s is now exhausted of funds ***\n\n", ledger_name);
        else
            printf ("*** The account %s is now over the allotted budget ***\n\n", ledger_name);
    
        /* Display of ledger record */
    
            printf ("*** Ledger Record ***\n\n");
            printf ("%s", ledger_name);
            printf ("\nStarting Balance: %.2f\n\n", current_balance);
        for (x = 1; x <= number_of_credits; x++)
        {
            printf ("Credit #%i: %.2f\n", x, credits[x]);
        }
        for (x = 1; x <= number_of_debits; x++)
        {
            printf ("\nDebit #%i: %.2f\n", x, debits[x]);
        }
            printf ("\nEnding Balance: $ %.2f\n", balance);
     
        return 0;
    }
    
    //end main

  14. #14
    Registered User
    Join Date
    Oct 2012
    Posts
    20
    I know the fflush is still in there we have started to learn gets but i dont really know how to use it yet. i am only in intro to c

  15. #15
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    The indentation still needs some work, but this is doable for now.

    I'm not seeing the "greater than 0" error you mentioned in post #6. Is that still happening? What are you entering?

    Please explain the trouble you are having. Be as specific as possible.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 1 Error I Cant Figure Out!! Tried All I Could Think Of!!
    By irishfeeney92 in forum C Programming
    Replies: 16
    Last Post: 04-30-2011, 04:17 PM
  2. Odd bus error, can't figure it out.
    By Subsonics in forum C Programming
    Replies: 15
    Last Post: 11-05-2009, 11:44 AM
  3. I cant figure out how to fix this error
    By Raigne in forum C++ Programming
    Replies: 4
    Last Post: 11-28-2005, 02:04 AM
  4. can't figure this error out
    By the Wookie in forum C++ Programming
    Replies: 4
    Last Post: 07-08-2003, 03:23 PM
  5. getting an error I can't figure out....
    By Soopafly in forum C++ Programming
    Replies: 3
    Last Post: 11-14-2002, 09:30 AM