Thread: Calculating weekly pay of employee (code not working right..)

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

    Angry Calculating weekly pay of employee (code not working right..)

    if you enter in 40 or less for the hour it works perfectly fine. As soon as it start calculating overtime time pay it messes up...

    any help would be appreciated.










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

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Perhaps the error is in your calculations?

    Say I enter 45 hours. Your program calculates pay1 = 45*9, but pay1 is maximum 40 hours worked.
    9 + 9/2 is 13 because of integer division, not 13.5 as it should be.

    Move some of the logic around to get it right.
    Last edited by whiteflags; 10-04-2017 at 04:28 AM.

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    Your variables are all ints. They should be doubles. Do you always get paid in even dollar amounts? Most people don't!! ;^)

    Also, please make all your variables local to main(). Globals are usually NOT recommended.

  4. #4
    Registered User
    Join Date
    Oct 2017
    Posts
    6
    Quote Originally Posted by rstanley View Post
    your variables are all ints. They should be doubles. Do you always get paid in even dollar amounts? Most people don't!! ;^)

    also, please make all your variables local to main(). Globals are usually not recommended.

    thank you so much, it worked after i changed it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New Employee Learning A Company's Code
    By dmanniteaux in forum Tech Board
    Replies: 4
    Last Post: 01-03-2013, 10:07 PM
  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