c - 为什么我的代码运行后显示空白?

标签 c

我是 C 的初学者,并开始自学它。 我这里的代码基本上是试图计算一个 worker 的每周工资 我必须输入没有。每周来自用户的小时数。 这是我正在研究的问题

In this challenge you are to create a C program that calculates you weekly pay.

The program should ask the user to end the number of hours worked in a week via the keyboard

The program should display as output the gross pay, the taxes, and the net pay.

The following assumptions should be made:

  • Basic pay rate = $12.00/hr
  • Overtime (in excess of 40 hours) = time and a half
  • Tax rate:

    • 15% of the first $300
    • 20% of the next $150
    • 25% of the rest

当我运行代码时,它要求我输入否。输入后仅显示空白的小时数 我知道它很长很简单,但我仍然面临问题

帮助非常感谢


#include <stdio.h>

int main(void)
{
    float hrs=0;
    float grossPay,taxes,netPay;
    float bpr=12.00;
    printf("The number of hours worked in a week: ");
    scanf("%f\n",&hrs);
    if (hrs<=40)
        grossPay=(hrs*bpr);
        printf("Gross Pay:%f\n",grossPay);
        if (grossPay<=300)
            taxes=0.15*grossPay;
            netPay=grossPay-taxes;
            printf("Total taxes:%f\n",taxes);
            printf("Net Pay:%f\n",netPay);
        if (grossPay>300 && grossPay<=450)
            taxes=0.15*300+0.20*(grossPay-300);
            netPay=grossPay-taxes;
            printf("Total taxes:%f\n",taxes);
            printf("Net Pay:%f\n",netPay);
        if (grossPay>450)
            taxes=0.15*300+0.20*150+0.25*(grossPay-450);
            netPay=grossPay-taxes;
            printf("Total taxes:%f\n",taxes);
            printf("Net Pay:%f\n",netPay);
    if (hrs>40)
        grossPay=40*bpr+18.00*(hrs-40);
        printf("Gross Pay:%f\n",grossPay);
        if (grossPay<=300)
            taxes=0.15*grossPay;
            netPay=grossPay-taxes;
            printf("Total taxes:%f\n",taxes);
            printf("Net Pay:%f\n",netPay);
        if (grossPay>300 && grossPay<=450)
            taxes=0.15*300+0.20*(grossPay-300);
            netPay=grossPay-taxes;
            printf("Total taxes:%f\n",taxes);
            printf("Net Pay:%f\n",netPay);
        if (grossPay>450)
            taxes=0.15*300+0.20*150+0.25*(grossPay-450);
            netPay=grossPay-taxes;
            printf("Total taxes:%f\n",taxes);
            printf("Net Pay:%f\n",netPay);


   return 0;
}

我为此做了一个替代代码。哪个更有效?:

#include <stdio.h>

#define PAYRATE 12.00
#define TAXRATE_300 .15
#define TAXRATE_150 .20
#define TAXRATE_REST .25
#define OVERTIME 40

int main()
{

   int hrs = 0;
   double grossPay = 0.0;
   double taxes = 0.0;
   double netPay = 0.0;

   printf("The number of hrs worked this week: ");

   scanf("%d", &hrs);


   if (hrs <= 40)
       grossPay = hrs * PAYRATE;
   else
   {
     grossPay = 40 * PAYRATE;
     double overTimePay = (hrs - 40) * (PAYRATE * 1.5);
     grossPay += overTimePay;
   }


   if (grossPay <= 300)
   {
       taxes = grossPay * TAXRATE_300;
   }
   else if(grossPay > 300 && grossPay <= 450)
   {
       taxes = 300 * TAXRATE_300;
       taxes += (grossPay - 300) * TAXRATE_150;
   }
   else if (grossPay > 450)
   {
       taxes = 300 * TAXRATE_300;
       taxes += 150 * TAXRATE_150;
       taxes += (grossPay - 450) * TAXRATE_REST;
   }


   netPay = grossPay - taxes;


   printf("Your gross pay this week is: %.2f\n", grossPay);
   printf("Your taxes this week is: %.2f\n", taxes);
   printf("Your net pay this week is: %.2f\n", netPay);

   return 0;
}

最佳答案

这是因为在对 scanf 的调用结束时有 \n --- 这需要它读取空白直到空白 block 结束(即 a非空白字符)。只需删除 \n

另外请注意,您的 if 语句的代码周围缺少大括号({, })。

关于c - 为什么我的代码运行后显示空白?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59522964/

相关文章:

python - 来自 C (Visual Studio) 的 CMD 命令

c - 在 C 中填充数组时对数组进行排序

c - Python C API : Passing two functions of many parameters with special types as module

c - 在 C 循环中打印 char 的输入

c - C 中的二次方程

c - 使用 read 读取时出现段错误

来自 argc 的 C 段错误

此修订版需要代码建议

更改函数内的指针

c - 在 C 数组表达式中使用时,如何理解 "add"(+) 运算符?