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("\n***Balanceiszero.Noadditionalwithdrawalsallowed.***\n");numWithdrawals=x;break;}/* End if *//* Enter amounts insuring they do not exceed current balance. */do{printf("Entertheamountofwithdrawal#%i:",x+1);scanf("%f",&withdrawals[x]);fflush(stdin);if(withdrawals[x]>currentBalance)printf("***Withdrawalamountexceedscurrentbalance,pleasere-enter.***\n");elseif(withdrawals[x]<=0)printf("***Withdrawalamountmustbegreaterthanzero.***\n");}while(withdrawals[x]>currentBalance||withdrawals[x]<=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[]);
}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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++ )
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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?
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("\n***Balanceiszero.Noadditionalwithdrawalsallowed.***\n");numWithdrawals=x;break;}/* End if *//* Enter amounts insuring they do not exceed current balance. */do{printf("Entertheamountofwithdrawal#%i:",x+1);scanf("%f",&withdrawals[x]);fflush(stdin);if(withdrawals[x]>currentBalance)printf("***Withdrawalamountexceedscurrentbalance,pleasere-enter.***\n");elseif(withdrawals[x]<=0)printf("***Withdrawalamountmustbegreaterthanzero.***\n");}while(withdrawals[x]>currentBalance||withdrawals[x]<=0);/* End do while */currentBalance=currentBalance-withdrawals[x];}/* End for loop */
}
int displayDeposits(int numDeposits, float deposits[])
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("\n\nWelcometotheACMEBankingSystem\n\n");printf("\nPleaseenteryourfirstname:");scanf("%s",firstName);fflush(stdin);printf("\n\nHello%s.\n\n",firstName);do{printf("\nNowenteryourcurrentbalanceindollarsandcents:");scanf("%f",&initialBalance);fflush(stdin);if(initialBalance<0)printf("Error:StartingBalancemustbeatleastzero.\n\n");}while(initialBalance<0);/* End do while */printf("\n");/* Prompt user for number of withdrawals */do{printf("\nEnterthenumberofwithdrawals:");scanf("%i",&numWithdrawals);fflush(stdin);if(numWithdrawals>50)printf("Error:Toomanywithdrawals,maximumoffiftyallowed.***\n");if(numWithdrawals<0)printf("Error:Numberofwithdrawalsmustbeatleastzero,pleasere-enter.\n");}while(numWithdrawals>50||numWithdrawals<0);/* End do while */printf("\n");/* Prompt user for number of deposits */do{printf("\nEnterthenumberofdeposits:");scanf("%i",&numDeposits);fflush(stdin);if(numDeposits>50)printf("Error:Toomanydeposits,maximumoffiftyallowed.\n");if(numDeposits<0)printf("Error:Numberofdepositsmustbeatleastzero,pleasere-enter.\n");}while(numDeposits>50||numDeposits<0);currentBalance=initialBalance;/* Set inital balance to equal current balance */printf("\n\n");/* Prompt for all deposits while keeping track of current balance */for(x=0;x<numDeposits;x++){do{printf("Entertheamountofdeposit#%i:",x+1);scanf("%f",&deposits[x]);fflush(stdin);if(deposits[x]<=0)printf("***Depositamountmustbepositive.Pleasere-enter.***\n");}while(deposits[x]<=0);/* End do while */currentBalance=currentBalance+deposits[x];}/* End for loop */printf("\n\n");promptWithdrawals(numWithdrawals,withdrawals);printf("\n\n");/* Display closing balance with message */printf("***Theclosingbalance%sis$%.2f***\n",firstName,currentBalance);if(currentBalance>=50000.00)printf("***%stimetoinvestsomemoney!***",firstName);elseif(currentBalance>=15000.00)printf("***%smaybeyoushouldconsideraCD.***",firstName);elseif(currentBalance>=1000.00)printf("***%skeepupthegoodwork!***",firstName);elseprintf("***%syourbalanceisverylow.***",firstName);/* End if else */printf("\n");/* Display bank record */printf("\n\n***BankRecord***\n");printf("\nStartingBalance:$%11.2f\n\n",initialBalance);displayDeposits(numDeposits,deposits);printf("\n");displayWithdrawals(numWithdrawals,withdrawals);printf("\n");printf("\nEndingBalance:$%11.2f\n\n",currentBalance);getchar();return(0);
} / End main /
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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...
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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 */
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("\n***Balanceiszero.Noadditionalwithdrawalsallowed.***\n");numWithdrawals=x;break;}/* End if *//* Enter amounts insuring they do not exceed current balance */do{printf("Entertheamountofwithdrawal#%i:",x+1);scanf("%f",&withdrawals[x]);fflush(stdin);if(withdrawals[x]>currentBalance)printf("***Withdrawalamountexceedscurrentbalance,pleasere-enter.***\n");elseif(withdrawals[x]<=0)printf("***Withdrawalamountmustbegreaterthanzero.***\n");}while(withdrawals[x]>currentBalance||withdrawals[x]<=0);/* End do while */currentBalance=currentBalance-withdrawals[x];}/* End for loop */returncurrentBalance;
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("\n\nWelcometotheACMEBankingSystem\n\n");printf("\nPleaseenteryourfirstname:");scanf("%s",firstName);fflush(stdin);printf("\n\nHello%s.\n\n",firstName);do{printf("\nNowenteryourcurrentbalanceindollarsandcents:");scanf("%f",&initialBalance);fflush(stdin);if(initialBalance<0)printf("Error:StartingBalancemustbeatleastzero.\n\n");}while(initialBalance<0);/* End do while */printf("\n");/* Prompt user for number of withdrawals */do{printf("\nEnterthenumberofwithdrawals:");scanf("%i",&numWithdrawals);fflush(stdin);if(numWithdrawals>50)printf("Error:Toomanywithdrawals,maximumoffiftyallowed.***\n");if(numWithdrawals<0)printf("Error:Numberofwithdrawalsmustbeatleastzero,pleasere-enter.\n");}while(numWithdrawals>50||numWithdrawals<0);/* End do while */printf("\n");/* Prompt user for number of deposits */do{printf("\nEnterthenumberofdeposits:");scanf("%i",&numDeposits);fflush(stdin);if(numDeposits>50)printf("Error:Toomanydeposits,maximumoffiftyallowed.\n");if(numDeposits<0)printf("Error:Numberofdepositsmustbeatleastzero,pleasere-enter.\n");}while(numDeposits>50||numDeposits<0);currentBalance=initialBalance;/* Set inital balance to equal current balance */printf("\n\n");/* Prompt for all deposits while keeping track of current balance */for(x=0;x<numDeposits;x++){do{printf("Entertheamountofdeposit#%i:",x+1);scanf("%f",&deposits[x]);fflush(stdin);if(deposits[x]<=0)printf("***Depositamountmustbepositive.Pleasere-enter.***\n");}while(deposits[x]<=0);/* End do while */currentBalance=currentBalance+deposits[x];}/* End for loop */printf("\n\n");/* Use of function to prompt for withdrawals while keeping track of current balance */promptWithdrawals(numWithdrawals,withdrawals);printf("\n\n");/* Display closing balance with message */printf("***Theclosingbalance%sis$%.2f***\n",firstName,currentBalance);if(currentBalance>=50000.00)printf("***%stimetoinvestsomemoney!***",firstName);elseif(currentBalance>=15000.00)printf("***%smaybeyoushouldconsideraCD.***",firstName);elseif(currentBalance>=1000.00)printf("***%skeepupthegoodwork!***",firstName);elseprintf("***%syourbalanceisverylow.***",firstName);/* End if else */printf("\n");/* Display bank record. */printf("\n\n***BankRecord***\n");printf("\nStartingBalance:$%11.2f\n\n",initialBalance);/* Use of function to display deposits */displayDeposits(numDeposits,deposits);printf("\n");/* Use of function to display withdrawals */displayWithdrawals(numWithdrawals,withdrawals);printf("\n");printf("\nEndingBalance:$%11.2f\n\n",currentBalance);getchar();return(0);
} / End main /
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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:
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!
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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 /
}
int main(void)
{
int x;
int numWithdrawals;
float withdrawals[50] = {0.00};
float currentBalance = {0.00};
promptWithdrawals (withdrawals[]);
}
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++ )
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
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>
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 /
}
int displayDeposits(int numDeposits, float deposits[])
{
int x;
for (x = 0; x < numDeposits; x++)
{
}
int displayWithdrawals(int numWithdrawals, float withdrawals[])
{
int x;
for (x = 0; x < numWithdrawals; 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];
} / End main /
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...
Thanks again Clifford.
No worries, I figured it out, currentBalance needed to be a global variable.
Nicolas
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
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...
include <stdio.h>
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 /
} / End promptWithdrawals /
void displayDeposits(int numDeposits, float deposits[])
{
int x;
for (x = 0; x < numDeposits; x++)
{
} / End displayDeposits /
void displayWithdrawals(int numWithdrawals, float withdrawals[])
{
int x;
for (x = 0; x < numWithdrawals; x++)
{
} / End displayWithdrawals /
int main(void)
{
int x;
int numDeposits;
int numWithdrawals;
float deposits[50] = {0.00};
float withdrawals[50] = {0.00};
float currentBalance;
float initialBalance;
char firstName[20];
} / End main /
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
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
I should have said that it compiles without a peep with default compile options.
Sorry for the lack of clarity
Wayne
... and since they are both indicative of actual errors, it is a strong argument for -Wall -Werror.