473,414 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Help with C code functions

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
6 1992
weaknessforcats
9,208 Expert Mod 8TB
You are defining functions inside main(). Maybe you omitted the } at the end of main()?
Nov 5 '08 #2
donbock
2,426 Expert 2GB
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
gmdune
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
469 Expert 256MB
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
r035198x
13,262 8TB
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
9,065 Expert Mod 8TB
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

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

Similar topics

2
by: gomerpyl3 | last post by:
I am trying to make a small system to record details for a small video / DVD shop - approx 200 members @ the mo I wanted it to be able to do the following: Check out a video / DVD Return...
7
by: Alan Bashy | last post by:
Please, guys, In need help with this. It is due in the next week. Please, help me to implement the functions in this programm especially the first three constructor. I need them guys. Please, help...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
4
by: Chris Travers | last post by:
Hi all; A few years ago, I set about porting a PHP application from MySQL to PostgreSQL, after realizing that MySQL wasn't going to be able to handle it. In order to do this, I built a light,...
8
by: metaperl | last post by:
Hi, I would like an IDE that shows me all methods and functions I can call on a particular data item. For instance, iter() can be called on any sequence, but it is not a method. Nonetheless,...
16
by: Rex | last post by:
Hi All - I have a question that I think MIGHT be of interest to a number of us developers. I am somewhat new to VIsual Studio 2005 but not new to VB. I am looking for ideas about quick and...
7
by: pauldepstein | last post by:
#include <iostream> using namespace std; int main() { extern "C" int f(int, int);
21
by: asif929 | last post by:
I need immediate help in writing a function program. I have to write a program in functions and use array to store them. I am not familiar with functions and i tried to create it but i fails to...
11
by: Brad Baker | last post by:
I'm building a small web application - I started out placing all my code in one file (config.aspx). As I continue to add code though it was becoming very unwieldy. After doing some searching...
16
by: gnawz | last post by:
I have a pagination function I am using in a file called functions.php as below<? //Pagination functions function getPagingQuery($sql, $itemPerPage = 10) { if (isset($_GET) && (int)$_GET > 0) ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.