Thread: Calculating weekly pay of employee (calculation off by 1....)

  1. #1
    Registered User
    Join Date
    Oct 2017
    Posts
    6

    Calculating weekly pay of employee (calculation off by 1....)

    when I enter any value over 40, the program's calculation seems to always be off by 1? any ideas as to why ?




    Code:
    
    
    #include <stdio.h>
    
    
    
    
    int main(int argc, const char * argv[]) {
    
    
    double hours; /*hours worked*/
    double pay1; /*hourly wage*/
    double pay2; /*overtime pay (1.5 the pay1)*/
    double overtime; /*amount acounted with overtime hours*/
        double overtimepay;
    
    
    
    printf("Enter in the amount of hours you've worked this week");
        scanf("%lf", &hours);
    
    
    
        pay1 = (hours * 9);
        pay2 = (9 + (9 / 2));
    
    
    
        overtimepay = (hours - 40) * pay2;
    
        overtime = pay1 + overtimepay;
    
        if (hours <= 40)
        {
    printf("Your weekly pay is %f\n", pay1);
        }
        else
        {
    printf("Your weekly pay is %f\n", overtime);
        }
        return 0;
    }
    
    

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Telling us
    - what you typed in for pay
    - what answer you got
    - what answer you expected
    would have been a bonus.


    > pay2 = (9 + (9 / 2));
    This will be done as integer division (even though pay2 is a double), so it will be rounded down.

    Perhaps
    pay2 = (9.0 + (9.0 / 2.0));
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculating weekly pay of employee (code not working right..)
    By Timmy_2short in forum C Programming
    Replies: 3
    Last Post: 10-04-2017, 09:22 AM
  2. Replies: 3
    Last Post: 12-06-2012, 01:09 PM
  3. weekly wage calculator
    By brian75 in forum C++ Programming
    Replies: 17
    Last Post: 01-12-2010, 09:50 PM
  4. my weekly schedule
    By ober in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 09-04-2002, 10:00 AM
  5. Weekly Programming Contest
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 04-09-2002, 10:52 PM

Tags for this Thread