Thread: Function Definition Help

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    9

    Function Definition Help

    So this is a project that requires us to use 6 user-defined functions and pointers. It is to basically work like an ATM. The program will need to withdraw, deposit, and or check the balance of the check, savings, and credit accounts

    The code below is a snippet of my entire code, I just posted enough so that what I need help on can be answered.

    Code:
    #define _CRT_SECURE_NO_DEPRECATE#include<stdio.h>
    #include<ctype.h>
    
    
    /* Displays the list of options available. Prompts for the user’s selection and sets the value of the selection*/
    void MainMenu(int *option);
    
    
    /*Asks the user which type of account they would like to access and sets the value of the selection*/
    void AccountMenu(char *acctType);
    
    
    /*Displays the user’s current account balance for the selected account*/
    void GetBalance(double balance);
    
    
    /*Prompts the user for the amount of the withdrawal, determines if there are sufficient funds and updates the selected account if funds are dispensed*/
    void MakeWithdrawal(double *balance, char acctType);
    
    
    /*Prompts the user for the amount of deposit and updates the selected account*/
    void MakeDeposit(double *balance);
    
    
    /*Asks the user if they want another transaction*/
    void Keepgoing(char *goAgain);
    
    
    int main( )
    {
        int    option;                
        char    acctType;            
        char    goAgain;            
        double    balance;
        double    check    = 850.00;    
        double    savings = 2500.00;
        double    credit    = -1850.00;
        
        do
        {
            MainMenu(&option);    
            switch (option)        
            {
                case 1:            
                    printf("=============================================================================");
                    printf("\n\nYou selected 'Check Account Balance'\n");
                    AccountMenu(&acctType);
                switch (acctType)
                {
                    case 'C':        
                        printf("=============================================================================");
                        printf("\n\nYou selected 'Checking'\n");
                        break;
                    case 'S':
                        printf("=============================================================================");
                        printf("\n\nYou selected 'Savings'\n");
                        break;
                    case 'R':
                        printf("=============================================================================");
                        printf("\n\nYou selected 'Credit'\n");
                        break;
                    default:        
                        printf("=============================================================================");
                        printf("\n\nYou have selected an option not supported.\n");
                        break;
                }        
                    break;
                case 2:                
                        printf("=============================================================================");
                        printf("\n\nYou selected 'Deposit'\n");
                        AccountMenu(&acctType);
                switch (acctType)
                {
                    case 'C':        
                        printf("=============================================================================");
                        printf("\n\nYou selected 'Checking'\n");
                        printf("How much would you like to deposit to your Checking account? $ ");
                        MakeDeposit(&balance);
                        check = check + balance;
                        printf("\nDeposited funds: $%.2lf\nNew Balance: $%.2lf\n",balance,check);
                        break;
                    case 'S':
                        printf("=============================================================================");
                        printf("\n\nYou selected 'Savings'\n");
                        printf("How much would you like to deposit to your Savings account? $ ");
                        MakeDeposit(&balance);
                        savings = savings + balance;
                        printf("\nDeposited funds: $%.2lf\nNew Balance: $%.2lf\n",balance,savings);
                        break;
                    case 'R':
                        printf("=============================================================================");
                        printf("\n\nYou selected 'Credit'\n");
                        printf("How much would you like to deposit to your Credit account? $ ");
                        MakeDeposit(&balance);
                        credit = credit + balance;
                        printf("\nDeposited funds: $%.2lf\nNew Balance: $%.2lf\n",balance,credit);
                        break;
                    default:
                        printf("=============================================================================");
                        printf("\n\nYou have selected an option not supported.\n");
                        break;
                }        
                    break;
                default:
                    printf("=============================================================================");
                    printf("\n\nYou have selected an option not supported.\n");
                    printf("=============================================================================");
                    break;
            }
        }while(Keepgoing(&goAgain),goAgain == 'Y');
    }
    void MainMenu(int *option)
    {
        printf("Choose action:\n\n");
        printf("1. Check Balance\n");
        printf("2. Deposit\n");
        printf("\nEnter number: ");
        scanf("%d",option);
    }
    void AccountMenu(char *acctType)
    {
        printf("Choose account:\n");
        printf("\nC - Checking");
        printf("\nS - Savings");
        printf("\nR - Credit\n");
        printf("\nEnter letter: ");
        scanf(" %c",acctType);
        *acctType = toupper(*acctType);
    }
    void GetBalance(double balance)
    {
        //Help with this function definition please.
    }
    /*
    void MakeWithdrawal(double *balance, char acctType)
    {
      
    }
    */
    void MakeDeposit(double *balance)
    {
        double  dpst = 0;
        scanf("%lf",&dpst);
        *balance = dpst;
    }
    void Keepgoing(char *goAgain)
    {
        printf("=============================================================================");
        printf("\n\nAnother transcation?\n");
        printf("Y or N: ");
        scanf(" %c",goAgain);
        *goAgain = toupper(*goAgain);
        printf("=============================================================================");
        printf("\n\n");
    }
    What I need help with is defining the function so that it will access the value of the correct account. Its different from the previous 2 and has stumped me. It was thinking just making balance = check in the case statement would be all I needed, but i need to use the user defined function (assignment requirement).
    I don't know what to put inside the function to get the desired account balance printed.

    GetBalance(balance);
    Calling the function just to test things didnt work out either seeing as I get an error as the program runs "The variable 'balance' is being used without being initialized.
    I though a & in the call would solve that but no.

    Suggestions?
    Attached Files Attached Files
    Last edited by Devon Smith; 03-17-2013 at 05:34 PM.

  2. #2
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    One of your classmates has already started a thread for this assignment.
    Code Assistance

    I see you already know this. Why start a new thread? Just continue with the comments already given in the other thread.
    Last edited by jwroblewski44; 03-17-2013 at 06:11 PM.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    9
    I haven't heard from the person, and don't see any help that pertained to what I need assistance with on. I struck out on my own and just need this last part.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    This variable is NOT likely needed
    Code:
    double    balance;
    getbalance is supposed to display one of the three dollar amounts.
    (check, savings, or credit)
    Or at least that is my guess of the assignment.



    Note: The name GetBalance is misleading DisplayBalance is more correct.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    9
    Without declaring the variable 'double', I get a laundry list of failures and compiling errors.

    GetBalance is how the assignment words the user-defined function, so that's not changing.

    getbalance is supposed to display one of the three dollar amounts.
    (check, savings, or credit)
    Correct, and thats what my trouble is, that and calling the function.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    How to call it for credit; the other two should be clear, now.

    Tim S.

    Code:
    GetBalance(credit);
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User
    Join Date
    Feb 2013
    Posts
    9
    Code:
    GetBalance(credit);
    Hard to believe that was all that was holding me back.

    Code:
    GetBalance(check); GetBalance(savings); GetBalance(credit); //each inside designated case statement
    
    void GetBalance(double balance)
    {
        printf("Current funds; $%.2lf\n",balance);
    }
    Your help was much appreciated Tim.
    I got it all the functionality of the program working, seemingly error free.
    Thank you again, very much.
    Last edited by Devon Smith; 03-17-2013 at 08:19 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 01-04-2013, 03:10 AM
  2. Definition of a function.
    By KOFI in forum C Programming
    Replies: 1
    Last Post: 04-13-2010, 10:57 AM
  3. Function Definition
    By CorX in forum C++ Programming
    Replies: 8
    Last Post: 05-14-2009, 11:56 AM
  4. function definition help
    By volk in forum C Programming
    Replies: 5
    Last Post: 02-05-2003, 07:47 PM
  5. Function definition
    By Chilli55 in forum C++ Programming
    Replies: 1
    Last Post: 02-22-2002, 12:37 PM