Need help with "Function"

This is a discussion on Need help with "Function" within the C Programming forums, part of the General Programming Boards category; Hello guys, I need to display this output on my screen but I don't know what's wrong with my formula. ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    10

    Need help with "Function"

    Hello guys, I need to display this output on my screen but I don't know what's wrong with my formula. It keeps displaying my salary that I entered even though I put get_bonus and get_nett_salary. Anyone here expert in C Programming and willing to help me? Thanks in advance.

    Code:
    printf("\nBonus: RM %.2f", get_bonus);
    printf("\nNett Salary: RM %.2f", get_nett_salary);

    Output that I need to display
    Code:
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    DATA ENTRY
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Enter staff ID: 100101
    Enter staff salary: RM 3000
    Enter total units sold: 7
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    SALARY SLIP
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Staff ID: 1001001
    Staff salary: RM 3000.00
    Units sold: 7
    Bonus: RM 1500.00
    Nett Salary: RM 4500.00

    Code:
    #include <stdio.h>
    
    
    float get_bonus();
    float get_nett_salary();
    float display();
    
    
    void main()
    {
        int id, unit;
        float salary;
    
    
        printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        printf("\n             DATA ENTRY                ");
        printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        printf("\nEnter staff ID: ");
        scanf("%d", &id);
        printf("Enter staff salary: RM ");
        scanf("%f", &salary);
        printf("Enter total units sold: ");
        scanf("%d", &unit);
        printf("\n\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        printf("\n             SALARY SLIP                ");
        printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        printf("\nStaff ID: %d", id);
        printf("\nStaff salary: RM %.2f", salary);
        printf("\nUnits sold: %d", unit);
        printf("\nBonus: RM %.2f", get_bonus);
        printf("\nNett Salary: RM %.2f", get_nett_salary);
    
    
    }
    float get_bonus()
    {
        int unit;
        float salary, get_bonus;
        if (unit > 10){
        get_bonus = salary * 0.70;
        return (get_bonus);
        }
        else if (unit >= 5 && unit <= 10){
        get_bonus = salary * 0.50;
        return (get_bonus);
        }
    }
    
    
    float get_nett_salary()
    {
        float get_bonus, salary, get_nett_salary;
        get_nett_salary = get_bonus + salary;
        return (get_nett_salary);
    }
    float display()
    {
        int id, unit;
        float salary, get_bonus, get_nett_salary, display;
        printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        printf("\n             SALARY SLIP                ");
        printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        printf("\nStaff ID: %d", id);
        printf("Staff salary: RM %.2f", salary);
        printf("Units sold: %d", unit);
        printf("Bonus: RM %.2f", get_bonus);
        printf("Nett Salary: RM %.2f", get_nett_salary);
        return (display);
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,658
    The two offending lines are not calling the function. They are printing the address of the functions (which, using the %f format, gives undefined behaviour).

    Note the changes I have made in red.
    Code:
    printf("\nBonus: RM %.2f", get_bonus());
    printf("\nNett Salary: RM %.2f", get_nett_salary());
    Depending on your compiler, if you had turned up warning levels, you would have received diagnostics about this.

    Also, main() returns int, not void.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    If the variables inside of the function are meant to be the same as the variables outside, then they should be parameters instead.
    Code:
    float get_bonus(    int unit,    float salary    );
    The way you have it set up now, you have one unit and salary and so forth inside of the function, and another outside of the function, and they are all different. The variables inside of the function are unitialized, so they can not be reliably used. However, as long as you have parameters, you can pass in the correct values from main.

    You have a secondary problem. Referring to a function name like that won't be useful until you learn what function pointers are. Functions need to be invoked to work.
    Code:
    float my_bonus = get_bonus();

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 08-19-2012, 07:15 AM
  2. Replies: 3
    Last Post: 03-28-2008, 12:44 AM
  3. Replies: 8
    Last Post: 03-10-2008, 12:57 PM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

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