首页 > 解决方案 > 我的代码在计算每周工资时为每个输出返回 0

问题描述

我已经将每小时的费率(bpr)设置为 12。我的目标是在用户输入的小时数下找到总工资、税金和净工资。此外,如果小时数超过 40,则费率增加 1.5。所以额外时间的费率是 18 小时而不是 12 小时。这是我的代码:

#include <stdio.h>

int main ()
{
    //program to calculate weekly pay

    int hrs = 0;
    int bpr = 12;
    double gross_pay = 0.0;
    double tax = 0.0;
    double net_pay = 0.0;

    printf("Enter the no of hours worked in a week: ");
    scanf("%d", &hrs);

    if (hrs <= 40)
        gross_pay =(bpr * hrs);
    else
    {
        gross_pay = (bpr * hrs);
        double overtimepay = (hrs - 40) * (bpr * 1.5);
        gross_pay += overtimepay;
    }


    if (gross_pay <= 300)
        tax = (0.15 * gross_pay);

    else if (gross_pay > 300 && gross_pay <= 450)
        tax = ((0.15 * 300) + 0.2 * (gross_pay - 300));
    else
        tax = ((0.15 * 300) + (0.2 * 150) + 0.25 * (gross_pay - 450));

    net_pay = (gross_pay - tax);

    printf("The gross pay is: %d\n",gross_pay);
    printf("The total tax amount is: %d\n",tax);
    printf("The net pay is: %d\n",net_pay);


    return 0;

}

代码每次输出都返回 0。故障在哪里?

标签: c

解决方案


您通过将类型错误的数据传递给printf().

%d是为了打印int,但你是通过double

double要通过打印printf(),您可能应该使用%for %g。(还有一些其他说明符可以double以不同的格式打印)


推荐阅读