Help with C code functions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gmdune
    New Member
    • Oct 2008
    • 4

    Help with C code functions

    Hello All,

    I'm having a problem compiling my code and I can't see why...can someone please have a look and see what I might be doing wrong? Also my compiler is spitting out the following:

    [machine1@cels15 9075 milasp5.c]% gcc milasp5.c
    one.c: In function ‘displayBankRec ord’:
    one.c:40: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token

    Thanks for your consideration

    Code:
    #include <stdio.h>
    
        float get_startingbalance(void);
        float getCntOfWithdrawls();
        float getCntOfDeposits();
        float getEachDeposit();
        float getEachWithdrawl();
        float checkBalance();
        float calcAndDisplayBalance();
        float displayBankRecord()
    
    int main()
    
    {
    
        /* Declare Variables */
        
        float deposits[50] = {0}; 
        float withdrawals[50] = {0};
        char  first_name[20] = {0};
        int   num_withdrawals, num_deposits, x;
        float start_bal;
        float current_balance = 0;
        float total_deposits = 0;
        float total_withdrawals = 0;
        float balance;
        
        /* Output initial greeting */
    
        printf("Welcome to the Banking System.\n\n");
        
        printf("Please enter your first name: ");
        scanf("%s", &first_name);
        fflush(stdin);
    
        printf("\nHello, %s.\n\n", first_name);
        
        /* GET STARTING BALANCE */
    
        float get_start_balance() {
        
            float start_bal
        
        do {
            
            printf("%s, Please enter your current balance in dollars and cents: "); /* Prompt user for current balance. */
            scanf("%f", &start_bal);
            fflush(stdin);
    
            if (start_bal < 0)
                printf("Invalid entry. Starting balance must be at least zero!\n\n");
    
        } while (start_bal < 0); 
        
        return start_bal;
        
    }   /* end function get starting balance */
        
        /* GET NUMBER OF WITHDRAWALS */
    
        float getCntOfWithdrawls() {
        
            float num_withdrawls
    
        do {
            
            printf ("\nEnter the number of withdrawals: ");
            scanf ("%i", &num_withdrawals);
            printf ("\n");
            fflush (stdin);
    
            if (num_withdrawals < 0 || num_withdrawals > 50)
                printf ("Error: Number of withdrawals must be between zero and 50, please re-enter!\n\n");
        
        } while (num_withdrawals < 0 || num_withdrawals > 50); 
        
        return num_withdrawls;
        
    }   /* end function number of withdrawls */
    
        /* GET NUMBER OF DEPOSITS */
    
        float getCntOfDeposits() {
        
            float num_deposits
        
        do {
            
            printf ("Enter the number of deposits: ");
            scanf ("%i",&num_deposits);
            printf ("\n");
            fflush (stdin);
            
            if ( num_deposits < 0 || num_deposits > 50)
                printf ("Error: Number of deposits must be between 0 and 50, please re-enter!\n\n");
        
        } while (num_deposits < 0 || num_deposits > 50); /* end do-while loop */
        
        return num_deposits;
    
    }   /* end function number of deposits */
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You are defining functions inside main(). Maybe you omitted the } at the end of main()?

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Originally posted by weaknessforcats
      You are defining functions inside main(). Maybe you omitted the } at the end of main()?
      When you fix that you'll be faced with a bunch of undefined variables as your no-arguments functions are no longer able to find the variables defined within main.

      The simplest way to fix this is to move those variable definitions out of main in order to make them global variables. The right way to fix this is to change the function declarations to take input arguments.

      Comment

      • gmdune
        New Member
        • Oct 2008
        • 4

        #4
        Re-posted

        Thank You All for your suggestions, I'm getting there, but I'm not there yet. I'm re-posting my code and the following error from my compiler:

        [machine1@cels15 9075 one.c]% gcc one.c
        one.c:154: warning: data definition has no type or storage class
        one.c:154: error: conflicting types for ‘current_balanc e’
        one.c:34: error: previous definition of ‘current_balanc e’ was here
        one.c:154: error: initializer element is not constant
        one.c: In function ‘checkBalance’:
        one.c:203: error: break statement not within loop or switch

        Code:
        #include <stdio.h>
        
            float deposits[50] = {0}; 
            float withdrawals[50] = {0};
            char  first_name[20] = {0};
            int   num_withdrawals, num_deposits, x;
            float start_bal;
            float current_balance = 0;
            float total_deposits = 0;
            float total_withdrawals = 0;
            float balance;
            float get_startingbalance(void);
            float getCntOfWithdrawls();
            float getCntOfDeposits();
            float getEachDeposit();
            float getEachWithdrawl();
            float checkBalance();
            float calcAndDisplayBalance();
            float displayBankRecord();
        
        int main (void)
        
        {
        <snip>   
        }    
            
            /* GET STARTING BALANCE */
        
            float get_start_balance() {
        <snip>
        }   /* end function get starting balance */
            
            /* GET NUMBER OF WITHDRAWALS */
        
            float getCntOfWithdrawls() {
        <snip>
        }   /* end function number of withdrawls */
        
            /* GET NUMBER OF DEPOSITS */
        
            float getCntOfDeposits() {
            
        <snip>
        }   /* end function number of deposits */
        
            /* GET EACH DEPOSIT */
        
            float getEachDeposit() {
            
                float num_deposits;
            
            for (x = 0; x < num_deposits; x++)
            {
                
            do {
                
                printf ("Enter the amount of deposit #%i: ", x + 1);
                scanf ("%f",&deposits[x]);
                fflush (stdin);
                
                if (deposits[x] <= 0)
                    printf ("*** Deposit amount must be greater than zero. Please re-enter! ***\n");
            
            } while (deposits[x] <= 0);
            
            total_deposits = total_deposits + deposits[x];
            
            } /* end for loop */
            
        }    /*end function get each deposit*/
            
            current_balance = total_deposits + start_bal;     
            
            /* Get Each Withdrawal */
            
            float getEachWithdrawl() {
            
                float num_withdrawls;
        
            for (x = 0; x < num_withdrawals; x++)
            {
                
            do {
                
                printf ("\n");
                printf ("Enter the amount of withdrawal #%i: ", x + 1);
                scanf ("%f",&withdrawals[x]);
                fflush (stdin);
                
                if (withdrawals[x] > current_balance)
                    printf ("***Withdrawal amount exceeds current balance.***\n");
                
                else
                    if (withdrawals[x] <= 0)
                    printf ("*** Withdrawal amout must be greater than zero. Please re-enter! ***");
                
            } while (withdrawals[x] > current_balance || withdrawals[x] <= 0); /* end do-while loop */
             
             total_withdrawals = total_withdrawals + withdrawals[x];
             
            } /* end for loop */
             
        }   /* end function get each withdrawl */
        
            /* Check Balance */
            
            float checkBalance() {
                
        <snip>        
        }   /* end function check balance */
            
            /* Calculate and Display Balance */
            
            float calcAndDisplayBalance() {
                
        <snip>    
        }   /*end function calc and display balance*/
            
            /* Calculate and display balance*/
            
            float displayBankRecord() {
                
        <snip>
                
        }   /* end function display bank record */
        Last edited by gmdune; Nov 5 '08, 10:05 PM. Reason: Error in original post

        Comment

        • boxfish
          Recognized Expert Contributor
          • Mar 2008
          • 469

          #5
          The line
          current_balance = total_deposits + start_bal;
          is not inside a function. I don't know which function it's supposed to go in, but it has to be in a function.
          This error
          one.c:203: error: break statement not within loop or switch
          says exactly what it means. You have a meaningless break statement inside of an if.
          Hope this helps some.
          Edit:
          You do know that you have to call these functions from main or your program will not use them, right?

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            gmdune, you are better off reading the error messages you are getting and trying to understand what they mean. You can't go on posting on the forum every time you get a compilation error. Better try to learn from the compiler errors and try to resolve the problems yourself.

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              Can I remind everyone here that we don't allow posting of complete solutions, particularly to course work questions. We have good reasons for this give in our posting guidelines.

              gmdune, it would be worth while you going and reading those guidelines before posting further as breaking them can lead to official warning and site bans (note this isn't a warning just a friendly piece of advice).

              As r035198x you should try to understand your compiler diagnostics or at the very least go to that line in your code and see what you can see that is wrong. You should also be aware that it is not uncommon for line given in a compiler diagnostic to actually be the line of code after the line with the error on it. The compiler gives the line at which it was able to detect that an error had been made not the line with the error.

              Comment

              Working...