Menu

Syntax Error

TrakerJon
2008-11-24
2012-09-26
  • TrakerJon

    TrakerJon - 2008-11-24

    Hey guys, this is function homework, I'm getting a syntax error...

    C:\ANSI_C\assign_5\test.c: In function `main':
    C:\ANSI_C\assign_5\test.c:49: error: syntax error before ']' token

    ...and I can't seem to figure it out??? I'm trying to have the function prompt for the number of withdrawals while keeping track of the balance. Suggestions, hints, guidance? Thank you in advance.

    include <stdio.h>

    / Prompt for all withdrawals while keeping track of current balance /

    int promptWithdrawals(int numWithdrawals, float withdrawals[], float currentBalance)

    {
    int x;
    for (x = 0; x < numWithdrawals; x++ )
    {
    / If balance drops to zero, break loop, no additional withdrawals, reset withdrawal
    count to display in bank record
    /

        if (currentBalance == 0)
        {
            printf (&quot;\n*** Balance is zero. No additional withdrawals allowed. ***\n&quot;);
            numWithdrawals = x;
            break;
        }    /* End if */
    
        /* Enter amounts insuring they do not exceed current balance. */
    
        do
        {
            printf (&quot;Enter the amount of withdrawal #%i: &quot;, x+1 );
            scanf   (&quot;%f&quot;, &amp;withdrawals[x]);
            fflush(stdin);
    
            if (withdrawals[x] &gt; currentBalance)
               printf (&quot;*** Withdrawal amount exceeds current balance, please re-enter. ***\n&quot;);
            else
            if (withdrawals[x] &lt;= 0)
               printf (&quot;*** Withdrawal amount must be greater than zero. ***\n&quot;);
    
        } while (withdrawals[x] &gt; currentBalance || withdrawals[x] &lt;= 0);    /* End do while */
    
        currentBalance = currentBalance - withdrawals[x];
    
    }    /* End for loop */
    

    }

    int main(void)

    {

    int x;
    int numWithdrawals;
    float withdrawals[50] = {0.00};
    float currentBalance = {0.00};

    promptWithdrawals (withdrawals[]);

    }

     
    • cpns

      cpns - 2008-11-25

      Modification to my earlier suggestion:

      for( i = 0; currentBalance > 0 && i < numWithdrawals; i++ )
      {
      withdrawals[i] = promptWithdrawal( currentBalance ) ;
      currentBalance -= withdrawals[i] ;
      }

      this will prevent promptWithdrawal() being called when balance is already zero, further simplifying promptWithDrawal() since it no longer has to test it.

      It might be more realistic to have an over-draught limit and test against that, perhaps:

      for( i = 0; currentBalance > -overdraughtLimit && i < numWithdrawals; i++ )

       
    • cpns

      cpns - 2008-11-24

      Are you sure that this is your code?

      The reason I ask is this line:

      > promptWithdrawals (withdrawals[]);

      is somewhat naive and does not demonstrate any of the understanding of the language that the preceeding code does.

      For starters promptWithdrawals() takes three parameters not one, and that is not how you pass an array in any case - loose the "[]".

      Even if you supplied the other arguments you would need to initialise numWithdrawals.

      currentBalance is not an array, you should not therefore use array initialiser syntax.

      For type consistency, float literal constants need a f suffix, eg:

      float currentBalance = 0.00f

      otherwise you are implicitly initialising a float with a double.

      Clifford

       
    • TrakerJon

      TrakerJon - 2008-11-24

      Thank you again as always Clifford,

      I now see that I made a couple of blatant errors and/or typos (the result of one too many Guinesses yesterday no doubt)...my program is compiling now but I'm not getting the desired results. It's not subtracting the withdrawal amounts from the running balance making the ending balance incorrect? There is a logic error no doubt...can you point it out to me?

      include <stdio.h>

      /* Functions */
      
      
      /* Prompt for all withdrawals while keeping track of current balance */
      

      int promptWithdrawals(int numWithdrawals, float withdrawals[])

      {
      int x;
      float currentBalance;
      for (x = 0; x < numWithdrawals; x++ )
      {
      / If balance drops to zero, break loop, no additional withdrawals, reset withdrawal
      count to display in bank record
      /

          if (currentBalance == 0)
          {
              printf (&quot;\n*** Balance is zero. No additional withdrawals allowed. ***\n&quot;);
              numWithdrawals = x;
              break;
          }    /* End if */
      
          /* Enter amounts insuring they do not exceed current balance. */
      
          do
          {
              printf (&quot;Enter the amount of withdrawal #%i: &quot;, x+1 );
              scanf   (&quot;%f&quot;, &amp;withdrawals[x]);
              fflush(stdin);
      
              if (withdrawals[x] &gt; currentBalance)
                 printf (&quot;*** Withdrawal amount exceeds current balance, please re-enter. ***\n&quot;);
              else
              if (withdrawals[x] &lt;= 0)
                 printf (&quot;*** Withdrawal amount must be greater than zero. ***\n&quot;);
      
          } while (withdrawals[x] &gt; currentBalance || withdrawals[x] &lt;= 0);    /* End do while */
      
          currentBalance = currentBalance - withdrawals[x];
      
      }    /* End for loop */
      

      }

      int displayDeposits(int numDeposits, float deposits[])

      {
      int x;
      for (x = 0; x < numDeposits; x++)
      {

      printf (&quot;Deposit #%2i:       %11.2f\n&quot;, x+1, deposits[x]);
      
      }
      

      }

      int displayWithdrawals(int numWithdrawals, float withdrawals[])

      {
      int x;
      for (x = 0; x < numWithdrawals; x++)
      {

      printf (&quot;\nWithdrawal #%2i:    %11.2f&quot;, x+1, withdrawals[x]);
      
      }
      

      }

      int main(void)

      {
      / Declare variables /

      int x;
      int numDeposits;
      int numWithdrawals;
      float deposits[50] = {0.00};
      float withdrawals[50] = {0.00};
      float initialBalance;
      float currentBalance;
      char firstName[20];

      /* Greet user */
      
      printf (&quot;\n\nWelcome to the ACME Banking System\n\n&quot;);
      
      printf(&quot;\nPlease enter your first name: &quot;);  
      scanf(&quot;%s&quot;, firstName);  
      fflush(stdin);
      
      printf(&quot;\n\nHello %s.\n\n&quot;, firstName);
      
      do
      {
          printf (&quot;\nNow enter your current balance in dollars and cents: &quot;);
          scanf   (&quot;%f&quot;, &amp;initialBalance);
          fflush(stdin);
      
          if (initialBalance &lt; 0)
              printf(&quot;Error: Starting Balance must be at least zero.\n\n&quot;);
      
      } while (initialBalance &lt; 0);    /* End do while */
      
      printf(&quot;\n&quot;);
      
      /* Prompt user for number of withdrawals */
      
      do
      {
          printf (&quot;\nEnter the number of withdrawals: &quot;);
          scanf   (&quot;%i&quot;, &amp;numWithdrawals);
          fflush(stdin);
      
          if (numWithdrawals &gt; 50)
             printf (&quot;Error: Too many withdrawals, maximum of fifty allowed. ***\n&quot;);
          if (numWithdrawals &lt; 0)
             printf (&quot;Error: Number of withdrawals must be at least zero, please re-enter.\n&quot;);
      
      } while (numWithdrawals &gt; 50 || numWithdrawals &lt; 0);    /* End do while */
      
      printf(&quot;\n&quot;);
      
      /* Prompt user for number of deposits */
      
      do
      {
          printf (&quot;\nEnter the number of deposits: &quot;);
          scanf   (&quot;%i&quot;, &amp;numDeposits);
          fflush(stdin);
      
          if (numDeposits &gt; 50)
             printf (&quot;Error: Too many deposits, maximum of fifty allowed.\n&quot;);
          if (numDeposits &lt; 0)
             printf (&quot;Error: Number of deposits must be at least zero, please re-enter.\n&quot;);
      
      } while ( numDeposits &gt; 50 || numDeposits &lt; 0);
      
      currentBalance = initialBalance;    /* Set inital balance to equal current balance */
      
      printf (&quot;\n\n&quot;);
      
      /* Prompt for all deposits while keeping track of current balance */
      
      for (x = 0; x &lt; numDeposits; x++ )
      {
          do
          {
              printf (&quot;Enter the amount of deposit #%i: &quot;, x+1 );
              scanf   (&quot;%f&quot;, &amp;deposits[x]);
              fflush(stdin);
      
              if (deposits[x] &lt;= 0)
              printf (&quot;*** Deposit amount must be positive. Please re-enter. ***\n&quot;);
      
          } while (deposits[x] &lt;= 0);    /* End do while */
      
          currentBalance = currentBalance + deposits[x];
      
      }    /* End for loop */
      
      printf (&quot;\n\n&quot;);
      
      promptWithdrawals (numWithdrawals, withdrawals);
      
      printf (&quot;\n\n&quot;);
      
      /* Display closing balance with message */
      
      printf (&quot;*** The closing balance %s is $%.2f ***\n&quot;, firstName, currentBalance);
      
      if (currentBalance &gt;= 50000.00 )
          printf (&quot;*** %s time to invest some money! ***&quot;, firstName);
      else
      if (currentBalance &gt;= 15000.00 )
          printf (&quot;*** %s maybe you should consider a CD. ***&quot;, firstName);
      else
      if (currentBalance &gt;= 1000.00 )
          printf (&quot;*** %s keep up the good work! ***&quot;, firstName);
      else
          printf (&quot;*** %s your balance is very low. ***&quot;, firstName);    /* End if else */
      
      printf (&quot;\n&quot;);
      
      /* Display bank record */
      
      printf (&quot;\n\n*** Bank Record ***\n&quot;);
      
      printf (&quot;\nStarting Balance: $%11.2f\n\n&quot;, initialBalance);
      
      displayDeposits (numDeposits, deposits);
      
      printf (&quot;\n&quot;);
      
      displayWithdrawals (numWithdrawals, withdrawals);
      
      printf (&quot;\n&quot;);
      
      printf (&quot;\nEnding Balance: $  %11.2f\n\n&quot;, currentBalance);
      
      getchar();
      
      return (0);
      

      } / End main /

       
    • TrakerJon

      TrakerJon - 2008-11-24

      It also appears that the program will allow withdrawals to go below a zero balance in error...it obviously relates to currentBalance in a couple of places but I don't quite see how to fix it. Guidance is greatly appreciated...

       
    • TrakerJon

      TrakerJon - 2008-11-24

      Thanks again Clifford.

      No worries, I figured it out, currentBalance needed to be a global variable.

      Nicolas

       
      • cpns

        cpns - 2008-11-24

        Thanks for getting back rather than making anyone work unnecessarily. However, you did find the problem, you may not have applied the best solution.

        Resorting to global data is often indicative of poor design. You might find this enlightening: http://www.embedded.com/columns/breakpoint/193101156?_requestid=19248 - It is aimed at embedded systems development, but that is only because embedded systems developers often believe that they have an excuse. There are no excuses in desktop computing.

        It is possible to write hundreds of thousands of lines of code and never resort to a global. The dangers are few perhaps in small programs like this, but lazy habits can be hard to shake later.

        Clifford

         
    • TrakerJon

      TrakerJon - 2008-11-25

      Clifford,

      Thank you, programming is very new to me, I wasn't aware of issues regarding global vs. local variables. Having said that I'm still having a problem with my program ending balance results as it relates to currentBalance. The following is what I have in that regard, it compiles but gives incorrect output. Suggestions and assistance are very much appreciated...

      /* This program will make some simple banking calculations */
      

      include <stdio.h>

      /* Function to prompt for withdrawals while keeping track of current balance */
      

      int promptWithdrawals(int numWithdrawals, float withdrawals[])

      {
      int x;
      float currentBalance;
      for (x = 0; x < numWithdrawals; x++ )
      {
      / If balance drops to zero, break loop, no additional withdrawals, reset withdrawal
      count to display in bank record
      /

          if (currentBalance == 0)
          {
              printf (&quot;\n*** Balance is zero. No additional withdrawals allowed. ***\n&quot;);
              numWithdrawals = x;
              break;
          }    /* End if */
      
          /* Enter amounts insuring they do not exceed current balance */
      
          do
          {
              printf (&quot;Enter the amount of withdrawal #%i: &quot;, x+1 );
              scanf   (&quot;%f&quot;, &amp;withdrawals[x]);
              fflush(stdin);
      
              if (withdrawals[x] &gt; currentBalance)
                  printf (&quot;*** Withdrawal amount exceeds current balance, please re-enter. ***\n&quot;);
              else
              if (withdrawals[x] &lt;= 0)
                  printf (&quot;*** Withdrawal amount must be greater than zero. ***\n&quot;);
      
          } while (withdrawals[x] &gt; currentBalance || withdrawals[x] &lt;= 0);    /* End do while */
      
          currentBalance = currentBalance - withdrawals[x];
      
      }    /* End for loop */
      
      return currentBalance;
      

      } / End promptWithdrawals /

      /* Function to display deposits */
      

      void displayDeposits(int numDeposits, float deposits[])

      {
      int x;
      for (x = 0; x < numDeposits; x++)
      {

          printf (&quot;Deposit #%2i:       %11.2f\n&quot;, x+1, deposits[x]);
      
      }
      

      } / End displayDeposits /

      /* Function to display withdrawals */
      

      void displayWithdrawals(int numWithdrawals, float withdrawals[])

      {
      int x;
      for (x = 0; x < numWithdrawals; x++)
      {

          printf (&quot;\nWithdrawal #%2i:    %11.2f&quot;, x+1, withdrawals[x]);
      
      }
      

      } / End displayWithdrawals /

      int main(void)

      {

      /* Declare variables */
      

      int x;
      int numDeposits;
      int numWithdrawals;
      float deposits[50] = {0.00};
      float withdrawals[50] = {0.00};
      float currentBalance;
      float initialBalance;
      char firstName[20];

      /* Greet user */
      
      printf (&quot;\n\nWelcome to the ACME Banking System\n\n&quot;);
      
      printf(&quot;\nPlease enter your first name: &quot;);  
      scanf(&quot;%s&quot;, firstName);  
      fflush(stdin);
      
      printf(&quot;\n\nHello %s.\n\n&quot;, firstName);
      
      do
      {
          printf (&quot;\nNow enter your current balance in dollars and cents: &quot;);
          scanf   (&quot;%f&quot;, &amp;initialBalance);
          fflush(stdin);
      
          if (initialBalance &lt; 0)
              printf(&quot;Error: Starting Balance must be at least zero.\n\n&quot;);
      
      } while (initialBalance &lt; 0);    /* End do while */
      
      printf(&quot;\n&quot;);
      
      /* Prompt user for number of withdrawals */
      
      do
      {
          printf (&quot;\nEnter the number of withdrawals: &quot;);
          scanf   (&quot;%i&quot;, &amp;numWithdrawals);
          fflush(stdin);
      
          if (numWithdrawals &gt; 50)
              printf (&quot;Error: Too many withdrawals, maximum of fifty allowed. ***\n&quot;);
          if (numWithdrawals &lt; 0)
              printf (&quot;Error: Number of withdrawals must be at least zero, please re-enter.\n&quot;);
      
      } while (numWithdrawals &gt; 50 || numWithdrawals &lt; 0);    /* End do while */
      
      printf(&quot;\n&quot;);
      
      /* Prompt user for number of deposits */
      
      do
      {
          printf (&quot;\nEnter the number of deposits: &quot;);
          scanf   (&quot;%i&quot;, &amp;numDeposits);
          fflush(stdin);
      
          if (numDeposits &gt; 50)
              printf (&quot;Error: Too many deposits, maximum of fifty allowed.\n&quot;);
          if (numDeposits &lt; 0)
              printf (&quot;Error: Number of deposits must be at least zero, please re-enter.\n&quot;);
      
      } while ( numDeposits &gt; 50 || numDeposits &lt; 0);
      
      currentBalance = initialBalance;    /* Set inital balance to equal current balance */
      
      printf (&quot;\n\n&quot;);
      
      /* Prompt for all deposits while keeping track of current balance */
      
      for (x = 0; x &lt; numDeposits; x++ )
      {
          do
          {
              printf (&quot;Enter the amount of deposit #%i: &quot;, x+1 );
              scanf   (&quot;%f&quot;, &amp;deposits[x]);
              fflush(stdin);
      
              if (deposits[x] &lt;= 0)
                  printf (&quot;*** Deposit amount must be positive. Please re-enter. ***\n&quot;);
      
          } while (deposits[x] &lt;= 0);    /* End do while */
      
          currentBalance = currentBalance + deposits[x];
      
      }    /* End for loop */
      
      printf (&quot;\n\n&quot;);
      
      /* Use of function to prompt for withdrawals while keeping track of current balance */
      
      promptWithdrawals (numWithdrawals, withdrawals);
      
      printf (&quot;\n\n&quot;);
      
      /* Display closing balance with message */
      
      printf (&quot;*** The closing balance %s is $%.2f ***\n&quot;, firstName, currentBalance);
      
      if (currentBalance &gt;= 50000.00 )
          printf (&quot;*** %s time to invest some money! ***&quot;, firstName);
      else
      if (currentBalance &gt;= 15000.00 )
          printf (&quot;*** %s maybe you should consider a CD. ***&quot;, firstName);
      else
      if (currentBalance &gt;= 1000.00 )
          printf (&quot;*** %s keep up the good work! ***&quot;, firstName);
      else
          printf (&quot;*** %s your balance is very low. ***&quot;, firstName);    /* End if else */
      
      printf (&quot;\n&quot;);
      
      /* Display bank record. */
      
      printf (&quot;\n\n*** Bank Record ***\n&quot;);
      
      printf (&quot;\nStarting Balance: $%11.2f\n\n&quot;, initialBalance);
      
      /* Use of function to display deposits */
      
      displayDeposits (numDeposits, deposits);
      
      printf (&quot;\n&quot;);
      
      /* Use of function to display withdrawals */
      
      displayWithdrawals (numWithdrawals, withdrawals);
      
      printf (&quot;\n&quot;);
      
      printf (&quot;\nEnding Balance: $  %11.2f\n\n&quot;, currentBalance);
      
      getchar();
      
      return (0);
      

      } / End main /

       
    • cpns

      cpns - 2008-11-25

      Does that code compile without warnings in Dev-C++? I cannot believe that it does. In VC++ 2008 it gives:

      main.c(44) : warning C4244: 'return' : conversion from 'float' to 'int', possible loss of data
      main.c(17) : warning C4700: uninitialized local variable 'currentBalance' used

      Both of those warnings are in fact errors. The compiler can warn against dodgy looking code, but if it is nonetheless valid C it has to compile it. However, as a programmer you need to consider the warnings and understand why the code is probably falwed. In fact I suggest that you use the following compiler options -Wall -Werror -Wformat. This will increase the 'fussiness' of the compiler so you can improve teh quality of teh code, it also makes all warnings errors (because they are!), and helpfully -Wformat, will check format specifiers in formatted I/O functions for you.

      promptWithdrawals() has been defined to return an int, yet you return a float. As it happens you simply discard the value when you call promptWithdrawals() at line 173 in any case! I am guessing that is a hangover from when currentBalance was global, you appear to be banging at the keyboard without really thinking.

      Despite my criticism of the global data solution, there was a problem that needed solving, and simply reverting it back to a local also undid the solution! You are not yet thinking like a programmer - but you will probably get there.

      Looking at it very briefly. I would suggest that you simplify promptWithdrawals() so that it prompts for just one withdrawal and returns its value (also rename it promptWithdrawal() singular rather than plural. Pass the current balance to promptWithdrawal() only so that it can check funds, not to maintain the balance. Its name does not suggest that it does anything other than getting a single withdrawal value, the side-effect of maintaining the balance and withdrawal list is just bad design - (look up the concepts of coupling and cohesion wrt software engineering). That way you need not pass the number of withdrawals or the withdrawals array, and you simply call promptWithdrawal() in a loop within main, and currentBalance is local to main() only; something like:

      for( i = 0; i < numWithdrawals; i++ )
      {
      withdrawals[i] = promptWithdrawal( currentBalance ) ;
      currentBalance -= withdrawals[i] ;
      }

      promptWithdrawal() itself should merely concern itself with obtaining a valid response from the user that does not exceed currentBalance and then return it - no more!

      With respect to coupling and cohesion mentioned earlier:
      http://en.wikipedia.org/wiki/Cohesion_(computer_science)
      http://en.wikipedia.org/wiki/Coupling_(computer_science)

      Clifford

       
    • Wayne Keen

      Wayne Keen - 2008-11-25

      Interestingly, it does compile under gcc-3.4.4 and gcc-4.2.1 (Cygwin and MinGW) without a peep. (I have had to go away from Dev here at work due to some path issues, so I compiled from the command line).

      Wayne

       
      • Wayne Keen

        Wayne Keen - 2008-11-25

        I should have said that it compiles without a peep with default compile options.

        Sorry for the lack of clarity

        Wayne

         
        • cpns

          cpns - 2008-11-25

          ... and since they are both indicative of actual errors, it is a strong argument for -Wall -Werror.

           

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.