skip to Main Content

i’m trying to make a calculation for wages and the requirements as below:

  • basic pay = 12.00 /hr
  • OT (>40hrs)=1.5x pay per hr
  • tax
  • 15% first 300
  • 20% 301<=payout<=450
  • 25% >450

I declare the variables as follows:

  • hours as hrs
  • gross payment as gross
  • taxed as tax
  • total payout after deduction as pymt

I import my header as this

#include <stdio.h>
#include <stdlib.h>

I define my constant as this

#define bsc 12.00
#define t300 0.15
#define t150 0.20
#define t_rest 0.25
#define OT 40

This is how i defined my function

int main()
{
    int hrs=0;
    double gross = 0.00;
    double tax = 0.00;
    double pymt = 0.00;
    printf("How many hours worked this week?n");
    scanf("%i", &hrs);

    if (hrs <= 40)
        pymt = hrs * bsc;
    else
    {
        pymt = 40 * bsc;
        double OTpymt = (hrs - 40) * (bsc * 1.5);
        pymt += OTpymt;
    }
    
    if (gross <= 300)
    {
        tax = gross * t300;
    }
    
    else if (gross > 300 && gross <= 450)
    {
        tax = 300 * t150;
        tax += (gross - 300) * t150;
    }

    else if (gross > 450)
    {
        tax = 300 * t300;
        tax += 150 * t150;
        tax += (gross - 450) * t_rest;

    }

    pymt = gross - tax;
    printf("ngross: %.2lfn", gross);
    printf("tax: %.2lfn", tax);
    printf("total pay: %.2lf", pymt);


    return 0;
}

When I debug and insert any value in user input, it always opt as 0.00.
and this is the image –>
image

I need help without changing my int variable to other data type? took me days but still unable to find out the solution. i’m currently using c17. I had 4 different operating systems (Win10, Win11, Debian, Mac12) and it gives same outcome.

note: i tried %.2f for the specifier, and it still not working at all. also the same as i declare my constant as double.

2

Answers


  1. Chosen as BEST ANSWER

    @Weather Vane posted the issue and solution:

    You have double gross = 0.00; and there is no further assignment to gross

    Thank you very much for the help.


  2. gross is always 0.0, then tax is always zero, and pymt becomes zero as well pymt = gross - tax;

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search