Thread: else/if error

  1. #16
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by jpatrick View Post
    i still am not understanding the overtime_hours_worked issue. am i not supposed to use the mod? how else can the program understand how many hours an employee has worked after his/her 40th work hour?
    How would you do it? Would you divide by 40 and take the remainder? That's what mod does. If you don't understand what the program is supposed to do, there is no way you can actually write the program. That's why I told you in my other post that you need to work it out on paper. Calculate it for a lot of different values (it's really easy). How many hours of overtime did they work if they worked:
    47 hours that week?
    53?
    75?
    88?
    100?

    Reply to me in this thread with the answer for those 5 numbers above, and tell me what operation you used to figure that out.

    EDIT: Just saw your post...disregard this if it works

  2. #17
    Registered User
    Join Date
    Feb 2012
    Posts
    40
    for some reason, i didn't think to use subtraction...

    i was initially thinking that using the mod would take, for example, 46 and divide it by 40, which would go into once and have a remainder of 6.

    but could i instead use (num_hours_worked - 40)

    or would it not make a difference and i'm just not conceptually grasping the issue? cause that wouldn't surprise me, i'm still very new to this..

  3. #18
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Well, look at the numbers I asked you to try in post #16. Try them with subtraction and mod. Both methods would give the right answer for 47 and 53. What about the rest of the numbers? Which method stops working? At what point does it stop working?

  4. #19
    Registered User
    Join Date
    Feb 2012
    Posts
    40
    right. it stops working after 75 because 40 goes into 80+ at least twice so the remainder resets.

  5. #20
    Registered User
    Join Date
    Feb 2012
    Posts
    40
    This is where i'm at now.

    Code:
    int main() {
        int num_hours_worked, employee_type, overtime_hours_worked;
        float weekly_pay, overtime_pay, pay_rate;
    
        printf("Are you and hourly employee (1) or a manager (0)?\n");
        scanf("%d", &employee_type);
    
        printf("How many hours did you work this week?\n");
        scanf("%d", &num_hours_worked);
    
        printf("What is your hourly pay rate?\n");
        scanf("%f", &pay_rate);
    
    
        if (employee_type == 0) {
    
            weekly_pay = (pay_rate * 50.0);}
    
        else if (employee_type == 1) {
    
             if (num_hours_worked <= 40)
    
                (weekly_pay = (pay_rate * num_hours_worked));}
             else {
                (overtime_hours_worked) = (num_hours_worked - 40);
                (overtime_pay) = ((pay_rate * 1.5) * (overtime_hours_worked));
    
                (weekly_pay) = (weekly_pay + overtime_pay);}
    
    
             printf("You will get paid $%f this week.\n", weekly_pay);
    
    
    
       return 0;
    }
    everything works well except overtime pay. do i need to redefine weekly_pay for the last block? regardless of the numbers i put in, if it's more than 40 hours of work, the pay comes back as $00.000000..

  6. #21
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You don't need to redefine it, but you do need to calculate it. Remember, you're only calculating that on the first 40 hours they worked. Also, you have a lot of unnecessary parentheses. It's pointless to put them around a single variable on it's own, and it's pointless to enclose a full expression in them, thus:
    Code:
    else if (employee_type == 1) {
        if (num_hours_worked <= 40)
            weekly_pay = pay_rate * num_hours_worked;
    }
    else {
        overtime_hours_worked = num_hours_worked - 40;
        overtime_pay = pay_rate * 1.5 * overtime_hours_worked;
        weekly_pay = // 40 hours of regular pay plus overtime pay

  7. #22
    Registered User
    Join Date
    Feb 2012
    Posts
    40
    the variable i was using for up to 40 hours of regular pay previously was weekly_pay. but weekly_pay = weekly_pay +overtime pay doesn't make any sense. While i understand that it doesn't make any sense, it is addition, and overtime_pay is predefined as overtime_pay = pay_rate * 1.5 * overtime_hours_worked;
    so why is the output coming back as $0?

    for example, if i put

    Code:
     weekly_pay = 40.0 * pay_rate + overtime_pay;
    why does the answer come back as $0.000? even if the formula is incorrect, shouldn't it still come back with a number higher than zero?
    Last edited by jpatrick; 02-24-2012 at 07:51 PM.

  8. #23
    Registered User
    Join Date
    Feb 2012
    Posts
    40
    i figured it out. it was really simple i was just being neglectful and not really paying super close attention.

    i really appreciate your help and patience holding my hand, Flame of the West.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 11-28-2011, 11:48 AM
  2. Replies: 4
    Last Post: 07-24-2011, 09:38 PM
  3. Replies: 1
    Last Post: 11-15-2010, 11:14 AM
  4. Replies: 3
    Last Post: 10-02-2007, 09:12 PM
  5. Replies: 1
    Last Post: 01-11-2007, 05:22 PM