Thread: commission/salary calculator problem

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    7

    commission/salary calculator problem

    hi guys, this is the question i have been given:

    A large company pays its salespeople on a commission basis. The sales people receive £200 per week, plus 9% of their gross sales for that week. For example, a salesperson that sells £5000 worth of merchandise in a week receives £200 plus 9% of £5000, or a total of £650. You have been supplied with a list of items sold by each salesperson. The values of these items are as follows:
    Item Value
    1 239.99
    2 129.75
    3 99.95
    4 350.89
    Develop a program that inputs one salesperson’s items sold for last week, calculates the salesperson’s earning and outputs text that displays the salesperson’s earnings.

    this is my code:
    Code:
            int item1,item2,item3,item4;
            float value1=239.99;
            float value2=129.75;
            float value3=99.95;
            float value4=350.89;
    
            printf("please enter the number of 'item 1' you have sold\n");
            scanf("%d",&item1);
    
            printf("please enter the number of 'item 2' you have sold\n");
            scanf("%d",&item2);
    
            printf("please enter the number of 'item 3' you have sold\n");
            scanf("%d",&item3);
    
            printf("please enter the number of 'item 4' you have sold\n");
            scanf("%d",&item4);
    
            float sales1,sales2,sales3,sales4,totalsales;
    
            sales1=item1*value1;
            sales2=item2*value2;
            sales3=item3*value3;
            sales4=item4*value4;
    
            totalsales=sales1+sales2+sales3+sales4;
    
            float commission;
            int weeklypay=200;
    
            commission=(9/100)*totalsales;
    
            float salary;
    
            salary=commission+weeklypay;
    
            printf("Your salary for this week is: %f\n", salary);
    the answer always comes out as 200.00000

    any ideas why? thanks

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Because these:

    Code:
    commission=(9/100)*totalsales;
    are ints. When you divide an int by an int, you get an int, in this case, 0. Here's how the compiler treats numbers:

    Code:
    11 // an int
    11.0 // a double
    11.0f // a float
    You should actually not use floats (or doubles) for money, because money is fixed precision, and you cannot force a float to round itself. The way floats are represented in binary means that any fraction that can't be produced by dividing 1 by 2 (0.5, 0.25, 0.125, etc) cannot be exactly represented. Try this:

    Code:
    #include <stdio.h>
    
    int main() {
            float i;
            for (i=0.0f; i<20; i+=0.1f) {
                    printf("%f\n",i);
            }
            return 0;
    }
    The output is not what you expect. Now imagine this is supposed to be adding dimes to a bank account -- you will get short changed eventually. You can, of course, continuously round the output, but eventually the discrepancy will work its way into the first couple of places. There are various infamous historical software failures which resulted from the mis-use of floats.

    Instead, use integer cents for money. If you want to output dollars:

    Code:
    	int cents = 507;
    	printf("$%d.%02d\n", cents/100, cents%100);
    Of course, you want to be precise with division on cents -- you don't want to round down, you want to round closest. So you can actually use a float for those calculations, then put the value back into an int, adding 0.5:

    Code:
    	float tmp = 507.0f/4.0f; // 126.75
    	cents = tmp + 0.5f; // should yeild 127
    	printf("%d\n", cents);
    If the decimal remainder is >=0.5, cents will be rounded up. If it's <0.5, cents will be rounded down.
    Last edited by MK27; 01-22-2012 at 10:48 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Apr 2011
    Location
    dust
    Posts
    70
    Declare all your varaibles at starting of the block.
    Do the commission calculation on float.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. calculator problem
    By darkphenom in forum C Programming
    Replies: 4
    Last Post: 02-18-2010, 01:18 AM
  2. help with salary calculator and loops
    By snooki in forum C++ Programming
    Replies: 1
    Last Post: 12-21-2009, 04:29 PM
  3. Calculator problem.
    By Mrzach3590 in forum C++ Programming
    Replies: 22
    Last Post: 08-03-2006, 12:03 AM
  4. Salary Range Calculator help please!! =(
    By bambino in forum C++ Programming
    Replies: 3
    Last Post: 04-20-2006, 06:17 PM