Like Tree8Likes
  • 1 Post By quzah
  • 3 Post By Salem
  • 2 Post By Adak
  • 1 Post By grumpy
  • 1 Post By CommonTater

How simple can a Function make life?

This is a discussion on How simple can a Function make life? within the C Programming forums, part of the General Programming Boards category; Hi everyone, this is my 2nd post. My first post I was working on a banking program, well unfortunately I ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    15

    How simple can a Function make life?

    Hi everyone, this is my 2nd post. My first post I was working on a banking program, well unfortunately I am still working on it. This is my final output on the bottom. It works fine I like it, but I want to simplify it with the use of functions. Primarily I just want to convert these 3 blocks of code below into functions and arrange it so the program will run smoothly. Any input would be greatly appreciated.

    1.
    Code:
    do
    	{
    		printf ("Now enter your current balance in dollars and cents: ");
    		scanf	("%f", &starting_balance);
    		fflush(stdin);
    
    		if (starting_balance < 0)
    			printf("Error: Starting Balance must be at least zero!\n\n");
    
    	} while (starting_balance < 0);
    2.
    Code:
    do
    	{
    		printf ("\nEnter the number of withdrawals: ");
    		scanf	("%i", &number_of_withdrawals);
    		fflush(stdin);
    
    		if (number_of_withdrawals > 50)
    			  printf ("*** Too many withdrawals. ***\n");
    		if (number_of_withdrawals < 0)
    			  printf ("*** Negative number of withdrawals not allowed. ***\n");
    
    	} while (number_of_withdrawals > 50 || number_of_withdrawals < 0);
    3.
    Code:
    do
    	{
    		printf ("\n\nEnter the number of deposits: ");
    		scanf	("%i", &number_of_deposits);
    		fflush(stdin);
    
    		if (number_of_deposits > 50)
    			  printf ("*** Too many deposits. ***\n");
    		if (number_of_deposits < 0)
    			  printf ("*** Negative number of deposits not allowed. ***\n");
    
    	} while ( number_of_deposits > 50 || number_of_deposits < 0);







    Code:
    #include <stdio.h>
    
    void	main (void)
    {
    
    			/* Variable Declarations */
    			/* --------------------- */
    
    	int     number_of_deposits, number_of_withdrawals, i;
    	float	amount_to_deposit[50], amount_to_withdraw[50];
    	float   starting_balance, current_balance;
        char    name[30];
    
    		/* Display an introduction header */
    		/* ------------------------------ */
    
    	printf ("Welcome to the Sears Bank Balancing System\n\n");
    
        printf("Please enter your first name: ");
        gets(name);                                  /* or use scanf("%s", name);  */
        fflush(stdin);
    
        printf("\nHello %s\n\n", name);
    
    	/* Query the user for the starting balance (starting_balance >= 0) */
    	/* ------------------------------------------------------------ */
    
    	do
    	{
    		printf ("Now enter your current balance in dollars and cents: ");
    		scanf	("%f", &starting_balance);
    		fflush(stdin);
    
    		if (starting_balance < 0)
    			printf("Error: Starting Balance must be at least zero!\n\n");
    
    	} while (starting_balance < 0);
    
    		  /* Prompt for the number of withdrawals                     */
    		  /* Ensure that user did not enter more than 50 withdrawals. */
    		  /* -------------------------------------------------------- */
    
    	do
    	{
    		printf ("\nEnter the number of withdrawals: ");
    		scanf	("%i", &number_of_withdrawals);
    		fflush(stdin);
    
    		if (number_of_withdrawals > 50)
    			  printf ("*** Too many withdrawals. ***\n");
    		if (number_of_withdrawals < 0)
    			  printf ("*** Negative number of withdrawals not allowed. ***\n");
    
    	} while (number_of_withdrawals > 50 || number_of_withdrawals < 0);
    
    
    		  /* Prompt for the number of deposits.                    */
    		  /* Ensure that user did not enter more than 50 deposits. */
    		  /* ----------------------------------------------------- */
    
    	do
    	{
    		printf ("\n\nEnter the number of deposits: ");
    		scanf	("%i", &number_of_deposits);
    		fflush(stdin);
    
    		if (number_of_deposits > 50)
    			  printf ("*** Too many deposits. ***\n");
    		if (number_of_deposits < 0)
    			  printf ("*** Negative number of deposits not allowed. ***\n");
    
    	} while ( number_of_deposits > 50 || number_of_deposits < 0);
    
    
    
    		  /* Retain starting balance for bank record. */
    		  /* ---------------------------------------- */
    
    	current_balance = starting_balance;
    
    		  /* Prompt for all deposit amounts, keeping track of current balance. */
    		  /* ----------------------------------------------------------------  */
    
    	printf ("\n");     /* spacing   */
    	for (i = 0; i < number_of_deposits; i++ )
    	{
    		do
    		{
    			printf ("Enter the amount of deposit #%i: ", i+1 );
    			scanf	("%f", &amount_to_deposit[i]);
    			fflush(stdin);
    
    			if (amount_to_deposit[i] <= 0)
    			  printf ("*** Deposit amount must be positive! Please re-enter ***\n");
    
    		} while (amount_to_deposit[i] <= 0);
    
    		current_balance = current_balance + amount_to_deposit[i];
    
    	} /* end for loop */
    
    
    		  /* Prompt for all withdrawal amounts, keeping track of current balance. */
    		  /* -------------------------------------------------------------------  */
    
    
    	printf ("\n");   /* spacing   */
    	for (i = 0; i < number_of_withdrawals; i++ )
    	{
    		/* Enter amounts, and be sure amount does not exceed current balance. */
    		/* ------------------------------------------------------------------ */
    
    		do
    		{
    			printf ("Enter the amount of withdrawal #%i: ", i+1 );
    			scanf	("%f", &amount_to_withdraw[i]);
    			fflush(stdin);
    
    			if (amount_to_withdraw[i] > current_balance)
    				  printf ("*** Withdrawal amount exceeds current balance. ***\n");
    			else
    			if (amount_to_withdraw[i] <= 0)
    				  printf ("*** Withdrawal amount must be  greater than zero! ***\n");
    
    		} while (amount_to_withdraw[i] > current_balance || amount_to_withdraw[i] <= 0);
    
    		current_balance = current_balance - amount_to_withdraw[i];
    
    		/* If Balance goes to zero, break the loop, no more withdrawals allowed, reset the
    			withdrawal count so the bank record prints properly! */
    	
    		if(current_balance == 0)
    		{
    			printf ("\n*** Balance is now zero.  No more withdrawals allowed! ***\n");
    			number_of_withdrawals = i + 1;
    			break;
    		}  /* end-if */
    
    	} /* end for loop.*/
    	printf ("\n");       /* spacing   */
    
    
    	/* Output closing balance and appropriate closing message. */
    	/* ------------------------------------------------------- */
    
    	printf ("*** The closing balance %s is $%.2f ***\n\n", name, current_balance);
    
    	if (current_balance >= 50000.00 )
    		printf ("*** %s time to invest some money! ***", name);
    	else
    	if (current_balance >= 15000.00 )
    		printf ("*** %s maybe you should consider a CD. ***", name);
    	else
    	if (current_balance >= 1000.00 )
    		printf ("*** %s keep up the good work. ***", name);
    	else
    		printf ("*** %s your balance is very low. ***", name);
    
      
    	/* Output bank record: Starting balance, deposits and withdrawals. */
    	/* -------------------------------------------------------------   */
    
    
    	printf ("\n\n");  /* spacing   */
    	printf ("   *** Bank Record ***\n");
    	printf ("Starting Balance: $%13.2f\n\n", starting_balance);
    
    	for (i = 0; i < number_of_deposits; i++ )
    		printf ("Deposit #%2i:     %15.2f\n", i+1, amount_to_deposit[i]);
    
    	if (number_of_deposits > 0)
    		printf ("\n");           /*  spacing   */
    
    	for (i = 0; i < number_of_withdrawals; ++i )
    		printf ("Withdrawal #%2i:  %15.2f\n", i+1, amount_to_withdraw[i]);
    
    	printf ("\nEnding Balance: $%15.2f\n\n", current_balance);
    
        getchar();
    
    } /* end main. */

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Take a moment to think about what each function should be doing. What information do they need to know about, and what information will they hand back? Once you sort that out, turning them into functions should be pretty clear to you.


    Quzah.
    AndrewHunter likes this.
    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Well you've got the unholy trinity in your code, perhaps you could write the same thing without those 3 deadly sins?
    SourceForge.net: Void main - cpwiki
    SourceForge.net: Gets - cpwiki
    SourceForge.net: Fflush - cpwiki
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by Salem View Post
    Well you've got the unholy trinity in your code, perhaps you could write the same thing without those 3 deadly sins?
    SourceForge.net: Void main - cpwiki
    SourceForge.net: Gets - cpwiki
    SourceForge.net: Fflush - cpwiki
    Blasphemy! No mention of cprogrammings FAQs?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    15
    lol....so I'm off to a bad start. Its sad Im doing this as a project for school. I passed this in last week got an B+, but the prof never mentioned anything in those pages as a matter of fact we lose points if we do not include fflush

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Fflush works only on outward streams. On input streams, it's undefined. Think of it like the plumbing in your home - you can flush the toilet, but trying to flush the faucet, is quite goofy.

    Sounds like yes, your instructor is WAY behind the times for C.

    Grouping your code in functions is easiest generally, when you think about the functional aspect of the code: is it for input, output, a certain type of calculation, or what?

    Congrats on completing your class. Now learn how C really should work! < smile >

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > but the prof never mentioned anything in those pages as a matter of fact we lose points if we do not include fflush
    If your "prof" is teaching you about fflush(stdin) and such like, then they really don't know C at all.
    Feel free to "play along" with them just to pass the course, but make sure you seek out plenty of other sources of information.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    What makes a good candidate for a function...

    1) It does 1 useful thing (not 30)
    2) It encapsulates data
    3) It is called from more than one location in your code
    4) It makes your code easier to maintain.

    Any 3 out of 4... make it a function.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    The whole point of functions is to break up a program into smaller bits that are easier to manage. Small functions that do one thing CAN BE easier to implement, easier to get right, easier to verify, and easier to reuse.

    There is an art to breaking large systems into smaller pieces though.

    It is possible to make functions so small that they are difficult to use (sort of like trying to build a house from grains of sand). Although obviously functions that are huge are hard to use, get right, etc. The trick is the Goldilocks approach to everything: not too big, not too small, just right. Working out what is "just right", however, takes thought.


    Naming functions is an important as what goes in them. For example, code that repeatedly calls a function named DrawLineOnScreen() is much easier to understand than it would be if it called a function named ScreenOperation27().
    Last edited by grumpy; 07-24-2011 at 03:46 AM.
    CommonTater likes this.
    Right 98% of the time, and don't care about the other 3%.

  10. #10
    Registered User
    Join Date
    Jul 2011
    Posts
    15
    Quote Originally Posted by quzah View Post
    Take a moment to think about what each function should be doing. What information do they need to know about, and what information will they hand back? Once you sort that out, turning them into functions should be pretty clear to you.


    Quzah.

    I don't want to do that. What I want is for you to use your extensive knowledge of C to do my homework for me, that way one day I'll get my degree and know absolutely nothing and still get a six figure salary............... j/k

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Alint808 View Post
    I don't want to do that. What I want is for you to use your extensive knowledge of C to do my homework for me, that way one day I'll get my degree and know absolutely nothing and still get a six figure salary............... j/k
    LOL... nothing like overstating the obvious

    Actually, you seem to be doing pretty well so far.... My only suggestion, based soley upon what little I've seen of your stuff, is that you should perhaps make a second pass over your code with an eye to simplifying it. In my experience, it's the simple stuff that works the best.
    AndrewHunter likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A Simple Life Game Code - Near the end ?
    By apolochaves in forum C Programming
    Replies: 6
    Last Post: 08-05-2010, 03:32 AM
  2. Replies: 19
    Last Post: 09-28-2009, 01:45 AM
  3. I have 10 days to make the biggest decision of my life
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 33
    Last Post: 12-05-2008, 11:31 AM
  4. Making a very simple Half-Life mod
    By bengreenwood in forum Game Programming
    Replies: 3
    Last Post: 10-27-2007, 10:56 PM
  5. life hands a lemon, make lemonade?
    By DarkViper in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 08-10-2003, 12:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21