By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,434 Members | 1,858 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,434 IT Pros & Developers. It's quick & easy.

Help with C code functions

P: 4
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@cels159075 milasp5.c]% gcc milasp5.c
one.c: In function ‘displayBankRecord’:
one.c:40: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token

Thanks for your consideration

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdio.h>
  3.  
  4.     float get_startingbalance(void);
  5.     float getCntOfWithdrawls();
  6.     float getCntOfDeposits();
  7.     float getEachDeposit();
  8.     float getEachWithdrawl();
  9.     float checkBalance();
  10.     float calcAndDisplayBalance();
  11.     float displayBankRecord()
  12.  
  13. int main()
  14.  
  15. {
  16.  
  17.     /* Declare Variables */
  18.  
  19.     float deposits[50] = {0}; 
  20.     float withdrawals[50] = {0};
  21.     char  first_name[20] = {0};
  22.     int   num_withdrawals, num_deposits, x;
  23.     float start_bal;
  24.     float current_balance = 0;
  25.     float total_deposits = 0;
  26.     float total_withdrawals = 0;
  27.     float balance;
  28.  
  29.     /* Output initial greeting */
  30.  
  31.     printf("Welcome to the Banking System.\n\n");
  32.  
  33.     printf("Please enter your first name: ");
  34.     scanf("%s", &first_name);
  35.     fflush(stdin);
  36.  
  37.     printf("\nHello, %s.\n\n", first_name);
  38.  
  39.     /* GET STARTING BALANCE */
  40.  
  41.     float get_start_balance() {
  42.  
  43.         float start_bal
  44.  
  45.     do {
  46.  
  47.         printf("%s, Please enter your current balance in dollars and cents: "); /* Prompt user for current balance. */
  48.         scanf("%f", &start_bal);
  49.         fflush(stdin);
  50.  
  51.         if (start_bal < 0)
  52.             printf("Invalid entry. Starting balance must be at least zero!\n\n");
  53.  
  54.     } while (start_bal < 0); 
  55.  
  56.     return start_bal;
  57.  
  58. }   /* end function get starting balance */
  59.  
  60.     /* GET NUMBER OF WITHDRAWALS */
  61.  
  62.     float getCntOfWithdrawls() {
  63.  
  64.         float num_withdrawls
  65.  
  66.     do {
  67.  
  68.         printf ("\nEnter the number of withdrawals: ");
  69.         scanf ("%i", &num_withdrawals);
  70.         printf ("\n");
  71.         fflush (stdin);
  72.  
  73.         if (num_withdrawals < 0 || num_withdrawals > 50)
  74.             printf ("Error: Number of withdrawals must be between zero and 50, please re-enter!\n\n");
  75.  
  76.     } while (num_withdrawals < 0 || num_withdrawals > 50); 
  77.  
  78.     return num_withdrawls;
  79.  
  80. }   /* end function number of withdrawls */
  81.  
  82.     /* GET NUMBER OF DEPOSITS */
  83.  
  84.     float getCntOfDeposits() {
  85.  
  86.         float num_deposits
  87.  
  88.     do {
  89.  
  90.         printf ("Enter the number of deposits: ");
  91.         scanf ("%i",&num_deposits);
  92.         printf ("\n");
  93.         fflush (stdin);
  94.  
  95.         if ( num_deposits < 0 || num_deposits > 50)
  96.             printf ("Error: Number of deposits must be between 0 and 50, please re-enter!\n\n");
  97.  
  98.     } while (num_deposits < 0 || num_deposits > 50); /* end do-while loop */
  99.  
  100.     return num_deposits;
  101.  
  102. }   /* end function number of deposits */
  103.  
  104.  
Nov 5 '08 #1
Share this Question
Share on Google+
6 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
You are defining functions inside main(). Maybe you omitted the } at the end of main()?
Nov 5 '08 #2

Expert 100+
P: 2,415
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.
Nov 5 '08 #3

P: 4
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@cels159075 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_balance’
one.c:34: error: previous definition of ‘current_balance’ 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

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdio.h>
  3.  
  4.     float deposits[50] = {0}; 
  5.     float withdrawals[50] = {0};
  6.     char  first_name[20] = {0};
  7.     int   num_withdrawals, num_deposits, x;
  8.     float start_bal;
  9.     float current_balance = 0;
  10.     float total_deposits = 0;
  11.     float total_withdrawals = 0;
  12.     float balance;
  13.     float get_startingbalance(void);
  14.     float getCntOfWithdrawls();
  15.     float getCntOfDeposits();
  16.     float getEachDeposit();
  17.     float getEachWithdrawl();
  18.     float checkBalance();
  19.     float calcAndDisplayBalance();
  20.     float displayBankRecord();
  21.  
  22. int main (void)
  23.  
  24. {
  25. <snip>   
  26. }    
  27.  
  28.     /* GET STARTING BALANCE */
  29.  
  30.     float get_start_balance() {
  31. <snip>
  32. }   /* end function get starting balance */
  33.  
  34.     /* GET NUMBER OF WITHDRAWALS */
  35.  
  36.     float getCntOfWithdrawls() {
  37. <snip>
  38. }   /* end function number of withdrawls */
  39.  
  40.     /* GET NUMBER OF DEPOSITS */
  41.  
  42.     float getCntOfDeposits() {
  43.  
  44. <snip>
  45. }   /* end function number of deposits */
  46.  
  47.     /* GET EACH DEPOSIT */
  48.  
  49.     float getEachDeposit() {
  50.  
  51.         float num_deposits;
  52.  
  53.     for (x = 0; x < num_deposits; x++)
  54.     {
  55.  
  56.     do {
  57.  
  58.         printf ("Enter the amount of deposit #%i: ", x + 1);
  59.         scanf ("%f",&deposits[x]);
  60.         fflush (stdin);
  61.  
  62.         if (deposits[x] <= 0)
  63.             printf ("*** Deposit amount must be greater than zero. Please re-enter! ***\n");
  64.  
  65.     } while (deposits[x] <= 0);
  66.  
  67.     total_deposits = total_deposits + deposits[x];
  68.  
  69.     } /* end for loop */
  70.  
  71. }    /*end function get each deposit*/
  72.  
  73.     current_balance = total_deposits + start_bal;     
  74.  
  75.     /* Get Each Withdrawal */
  76.  
  77.     float getEachWithdrawl() {
  78.  
  79.         float num_withdrawls;
  80.  
  81.     for (x = 0; x < num_withdrawals; x++)
  82.     {
  83.  
  84.     do {
  85.  
  86.         printf ("\n");
  87.         printf ("Enter the amount of withdrawal #%i: ", x + 1);
  88.         scanf ("%f",&withdrawals[x]);
  89.         fflush (stdin);
  90.  
  91.         if (withdrawals[x] > current_balance)
  92.             printf ("***Withdrawal amount exceeds current balance.***\n");
  93.  
  94.         else
  95.             if (withdrawals[x] <= 0)
  96.             printf ("*** Withdrawal amout must be greater than zero. Please re-enter! ***");
  97.  
  98.     } while (withdrawals[x] > current_balance || withdrawals[x] <= 0); /* end do-while loop */
  99.  
  100.      total_withdrawals = total_withdrawals + withdrawals[x];
  101.  
  102.     } /* end for loop */
  103.  
  104. }   /* end function get each withdrawl */
  105.  
  106.     /* Check Balance */
  107.  
  108.     float checkBalance() {
  109.  
  110. <snip>        
  111. }   /* end function check balance */
  112.  
  113.     /* Calculate and Display Balance */
  114.  
  115.     float calcAndDisplayBalance() {
  116.  
  117. <snip>    
  118. }   /*end function calc and display balance*/
  119.  
  120.     /* Calculate and display balance*/
  121.  
  122.     float displayBankRecord() {
  123.  
  124. <snip>
  125.  
  126. }   /* end function display bank record */
  127.  
  128.  
Nov 5 '08 #4

boxfish
Expert 100+
P: 469
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?
Nov 5 '08 #5

10K+
P: 13,264
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.
Nov 6 '08 #6

Banfa
Expert Mod 5K+
P: 8,916
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.
Nov 6 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.