Thread: c program help - functions

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    6

    c program help - functions

    Hello, I need some help with the program included below.

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main(void)
    {
                  
                  /* Declare Variables */
    			  /* ----------------- */
    
    	int x, number_of_deposits, number_of_withdrawals;
    	float deposits[50], withdrawals[50];
    	float current_balance;
    	float balance;
    	float total_deposits = 0, total_withdrawals = 0;
        char    first_name[20];
        
              /* Output initial greeting */
              /* ------------------------*/
            
            printf("Welcome to the Sears Banking System.\n\n");
            printf ("Please enter your first name: ");
            scanf ("%s", first_name);
            fflush(stdin);
            printf("\nHello, %s.\n\n", first_name);   
            
            /* Prompt user for current balance in dollars and cents. */
            /* ----------------------------------------------------- */
    
      do
      {
    
    		printf ("Please enter your current balance in dollars and cents: ");
    		scanf ("%f",&current_balance);
    		fflush(stdin);
    
    
    		if (current_balance < 0)
    			printf ("Error: Beginning balance must be at least zero, please re-enter!\n\n");
    
      } while (current_balance < 0); /* end do-while loop */
    
            /* Prompt user for the number of withdrawals */
    	    /* ----------------------------------------- */
    
      do
      {
    
    		printf ("\nEnter the number of withdrawals: ");
    		scanf ("%i",&number_of_withdrawals);
    		fflush (stdin);
    
    
    		if (number_of_withdrawals < 0 || number_of_withdrawals > 50)
    			printf ("Error: Number of withdrawals must be between zero and 50, please re-enter!\n\n");
    
      } while (number_of_withdrawals < 0 || number_of_withdrawals > 50); /* end do-while trap loop */
    
            /* Prompt user to enter the number of deposits. */
    	    /* -------------------------------------------- */
    
      do
      {
    
    		printf ("\nEnter the number of deposits: ");
    		scanf ("%i",&number_of_deposits);
    		fflush (stdin);
    
    		if ( number_of_deposits < 0 || number_of_deposits > 50)
    			printf ("Error: Number of deposits must be between 0 and 50, please re-enter!\n\n");
    
      } while (number_of_deposits < 0 || number_of_deposits > 50); /* end do-while trap loop */
      
      printf("\n");
    		
            /* Prompt user for positive deposits and withdrawals. */
    	    /* -------------------------------------------------- */
    
      for (x = 1; x <= number_of_deposits; x++)
      {
    
    	 do
    	{
    
    		printf ("Enter the amount of deposit #%i: ", x);
    		scanf ("%f",&deposits[x]);
    		fflush (stdin);
    
    		if (deposits[x] <= 0)
    			printf ("*** Deposit amount must be greater than zero. Please re-enter! ***\n");
    
    	} while (deposits[x] <= 0);
    
      total_deposits = total_deposits + deposits[x];
    
      }// end for loop
      
      printf("\n");
    
      for (x = 1; x <= number_of_withdrawals; x++)
      {
    	  do
    	  {
    		  printf ("Enter the amount of withdrawal #%i: ", x);
    		  scanf ("%f",&withdrawals[x]);
    		  fflush (stdin);
    
    		  if (withdrawals[x] > current_balance)
    				printf ("***Withdrawal amount exceeds current balance.***\n");
    		  else
    		  if (withdrawals[x] <= 0)
    				printf ("*** Withdrawal amout must be greater than zero. Please re-enter! ***");
    
    	} while (withdrawals[x] > current_balance || withdrawals[x] <= 0); /* end do-while loop */
    
      total_withdrawals = total_withdrawals + withdrawals[x];
      
            /* If balance goes to zero, reset the withdrawal count */
    		/* -------------------------------------------------- */
    
      balance = current_balance - total_withdrawals + total_deposits;
    
    	if (balance == 0)
    	{
    		 printf ("\n *** Balance is now zero. No more withdrawals can be made at this time. ***\n");
    		 number_of_withdrawals = x;
    		 break;
    
    	} // end-if
    
      } //end for loop
    
    
    		/* Calculate and display the closing balance */
    		/* ---------------------------------------- */
    
    	printf ("\n*** Your closing balance is $%.2f, %s*** \n", balance, first_name);
    
    
    	if (balance >= 5000.00)
    		printf ("*** Time to invest some money!*** \n\n");
    	else if (balance >= 15000.00 && balance <= 49999.99)
    		printf ("*** Maybe you should consider a CD.*** \n\n");
    	else if (balance >= 1000.00 && balance <= 14999.99)
    		printf ("*** Keep up the good work.*** \n\n");
    	else
    		printf ("*** %s, your balance is getting low!*** \n\n", first_name);
    
    		/* Display bank record */
    		/* ------------------ */
    	
        printf ("     *** Bank Record ***\n");
    
    	printf ("\nStarting Balance:$   %13.2f\n\n", current_balance);
    
    
    	for (x = 1; x <= number_of_deposits; x++)
    	{
    	 printf ("Deposit #%i:          %13.2f\n", x, deposits[x]);
    	}
    
    
    	for (x = 1; x <= number_of_withdrawals; x++)
    	{
    	 printf ("\nWithdrawal  #%i:      %13.2f", x, withdrawals[x]);
    	}
    
    
    	printf ("\n\nEnding Balance is:$  %13.2f\n", balance);
    	
    	{
         getch();
         return 0;
        }
    } /*end main*/
    I need to alter this program so that it uses at least three "non-trivial" functions (non-trivial meaning that it does more than just print a line of text). My problem is that the text book I am using isn't very helpful so any help would be much appreciated.

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I need to alter this program so that it uses at least three "non-trivial" functions (non-trivial meaning that it does more than just print a line of text).
    The most likely candidates are those portions of code that you have begun with a useful comment like "Prompt user for current balance in dollars and cents." and "Calculate and display the closing balance".

    Incidentally, you need to indent your code more consistently, and note that fflush(stdin) is undefined.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You're also reading strings in an unsafe way. See http://cpwiki.sourceforge.net/Buffer...2a2eaeb5955006
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    6
    Hello, This is what I have so far.

    Code:
    #include <stdio.h>
    
    float get_startingbalance(void);
    float getCntOfWithdrawls();
    float getCntOfDeposits();
    float getEachDeposit();
    float getEachWithdrawl();
    float checkBalance();
    float calcAndDisplayBalance();
    float displayBankRecord()
    void print_withdrawalnumber(int num_withdrawals);
    void print_depositnumbers(int num_deposits);
    void print_startingbalance(float start_balance);
    void print_endbalance(float current_balance);
    int main()
    {
    /* Declare Variables */
    int num_withdrawals, num_deposits, x;
    float deposits[50] = {0.0}; 
    float withdrawals[50] = {0.0}; 
    float current_balance = {0.0};
    float start_balance = {0.0};
    char first_name[20];
    
    /* Output initial greeting */
    printf("Welcome to the Sears Banking System.\n");
    printf("Please enter your first name: ");
    scanf("&#37;s", first_name);
    fflush(stdin);
    printf("\nHello, %s\n\n", first_name);
    
    /* Prompt user for current balance. */
    
    start_balance = get_startingbalance();
    
    printf("You entered %.2f ", start_balance);
    
    getchar();
    
    
    
    } /* end main */
    /*START FUNCTION GET STARTING BALANCE*/
    
    float get_startingbalance()
    {
    float start_bal;
    do
    {
    printf("Please enter your current balance in dollars and cents: ");
    scanf("%f", &start_bal);
    fflush(stdin);
    if(start_bal < 0)
    printf("Invalid entry. Starting balance must be at least zero!\n\n");
    }while(start_bal < 0); /*END DO WHILE*/
    
    return start_bal;
    
    } /* end function get starting balance */
    /*START FUNCTION GET NUMBER OF WITHDRAWLS*/
    
    float getCntOfWithdrawls()
    {
    float num_withdrawls
    do
    {
    printf ("\nEnter the number of withdrawals: ");
    scanf ("%i",&num_withdrawals);
    fflush (stdin);
    if (num_withdrawals < 0 || num_withdrawals > 50)
    printf ("Error: Number of withdrawals must be between zero and 50, please re-enter!\n\n");
    } while (num_withdrawals < 0 || number_withdrawals > 50); /* end do-while trap loop */
    
    return num_withdrawls;
    
    }/* end function number of withdrawls */
    /*START FUNCTION GET NUMBER OF DEPOSITS*/
    
    float getCntOfDeposits()
    {
    float num_deposits
    do
    {
    printf ("\nEnter the number of deposits: ");
    scanf ("%i",&num_deposits);
    fflush (stdin);
    if ( num_deposits < 0 || number_deposits > 50)
    printf ("Error: Number of deposits must be between 0 and 50, please re-enter!\n\n");
    } while (number_deposits < 0 || number_deposits > 50); /* end do-while trap loop */
      
    return num_deposits;
    
    }/* end function number of deposits */
    /*START FUNCTION GET EACH DEPOSIT*/
    
    float getEachDeposit()
    {
    float num_deposits
    for (x = 1; x <= num_deposits; x++)
    {
    do
    {
    printf ("Enter the amount of deposit #%i: ", x);
    scanf ("%f",&deposits[x]);
    fflush (stdin);
    if (deposits[x] <= 0)
    printf ("*** Deposit amount must be greater than zero. Please re-enter! ***\n");
    } while (deposits[x] <= 0);
    total_deposits = total_deposits + deposits[x];
    }/*end for loop*/
    /*end function get each deposit*/
    /*START FUNCTION GET EACH WITHDRAWL*/
    
    float getEachWithdrawl()
    {
    float num_withdrawls
    for (x = 1; x <= num_withdrawals; x++)
    {
    do
    {
    printf ("Enter the amount of withdrawal #%i: ", x);
    scanf ("%f",&withdrawals[x]);
    fflush (stdin);
    if (withdrawals[x] > current_balance)
    printf ("***Withdrawal amount exceeds current balance.***\n");
    else
    if (withdrawals[x] <= 0)
    printf ("*** Withdrawal amout must be greater than zero. Please re-enter! ***");
    } while (withdrawals[x] > current_balance || withdrawals[x] <= 0); /* end do-while loop */
    total_withdrawals = total_withdrawals + withdrawals[x];
    /*end function get each withdrawl*/
    /*START FUNCTION CHECK BALANCE*/
    
    float checkBalance()
    {
    float current_balance
    balance = current_balance - total_withdrawals + total_deposits;
    if (balance == 0)
    {
    printf ("\n *** Balance is now zero. No more withdrawals can be made at this time. ***\n");
    num_withdrawals = x;
    break;
    } /*end-if*/
    } /*end for loop*/
    /*end function check balance*/
    /*START FUNCTION CALC AND DISPLAY BALANCE*/
    
    float calcAndDisplayBalance()
    {
    printf ("\n*** Your closing balance is $%.2f, %s*** \n", balance, first_name);
    if (balance >= 5000.00)
    printf ("*** Time to invest some money!*** \n\n");
    else if (balance >= 15000.00 && balance <= 49999.99)
    printf ("*** Maybe you should consider a CD.*** \n\n");
    else if (balance >= 1000.00 && balance <= 14999.99)
    printf ("*** Keep up the good work.*** \n\n");
    else
    printf ("*** %s, your balance is getting low!*** \n\n", first_name);
    }/*end-if*/
    /*end function calc and display balance*/
    /*START FUNCTION DISPLAY BANK RECORD*/
    
    float displayBankRecord()
    printf ("     *** Bank Record ***\n");
    printf ("\nStarting Balance:$   %13.2f\n\n", current_balance);
    for (x = 1; x <= number_of_deposits; x++)
    {
    printf ("Deposit #%i:          %13.2f\n", x, deposits[x]);
    }
    for (x = 1; x <= number_of_withdrawals; x++)
    {
    printf ("\nWithdrawal  #%i:      %13.2f", x, withdrawals[x]);
    }
    printf ("\n\nEnding Balance is:$  %13.2f\n", balance);
    /*end function display bank record*/
    Can someone tell me if I am on the right track? I have to admit that using function is really confusing me for some reason.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Your code is unindented and therefore unreadable.
    You're also flushing stdin, which is undefined (bad). Do read: http://cpwiki.sourceforge.net/Common...flush_stdin.21
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program with Shapes using Virtual Functions
    By goron350 in forum C++ Programming
    Replies: 12
    Last Post: 07-17-2005, 01:42 PM
  2. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 05:23 PM
  3. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  4. Help starting a C program with functions
    By jlmac2001 in forum C Programming
    Replies: 6
    Last Post: 10-12-2002, 02:43 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM