-2

有人能告诉我为什么这段代码不计算总工资和其他代码吗?这段代码中存在一个问题,它不像程序想要的那样计算。有人可以帮我查明错误吗?

#include <stdio.h>
#include <conio.h>

int main()
{
  char fname[20];
  char lname[20];
  char IDnumber[20];
  char Position;
  float Hoursworked,Grosspay,Tax,Netpay;

  printf(" ************iG Company************** \n\n");
  printf("Enter the employee's First Name:\n");
  scanf("%s", &fname);
  printf("Enter the employee's Last Name:\n");
  scanf("%s", &lname);
  printf("Enter IDnumber:\n");
  scanf("%s",&IDnumber);
  printf("<1>Manager <2>Supervisor heart>Employee <4>Office Boy/girl \n");
  printf("Enter Position:\n");
  scanf("%s", &Position);
  switch(Position)
  {
    case '1':
      printf("Rate per hour:P150.00 \n");
      printf("No. of hours worked:");
      scanf("%d", &Hoursworked);
      Grosspay = Hoursworked * 150;
      printf("Gross: \n", Grosspay);
      Tax = Grosspay * 0.1f;
      printf("TAX:\n", Tax);
      Netpay = Grosspay - Tax;
      printf("NET PAY: \n", Netpay);
      break;
    case '2':
      printf("Rate:P100.00 \n");
      printf("No. of hours worked:");
      scanf("%d", &Hoursworked);
      Grosspay = Hoursworked * 100;
      printf("Gross:\n", Grosspay);
      Tax = Grosspay * 0.1f;
      printf("TAX: \n", Tax);
      Netpay = Grosspay-Tax;
      printf("NET PAY: \n", Netpay);
      break;
    case '3':
      printf("Rate:P70.00 \n");
      printf("No. of hours worked:");
      scanf("%d", &Hoursworked);
      Grosspay = Hoursworked * 70;
      printf("Gross: \n", Grosspay);
      Tax = Grosspay * 0.1f;
      printf("TAX:\n", Tax);
      Netpay = Grosspay - Tax;
      printf("NET PAY: \n", Netpay);
      break;
    case '4':
      printf("Rate per hour:P50.00 \n");
      printf("No. of hours worked:");
      scanf("%d", &Hoursworked);
      Grosspay = Hoursworked * 50;
      printf("Gross:\n", Grosspay);
      Tax = Grosspay * 0.1f;
      printf("TAX:\n", Tax);
      Netpay = Grosspay - Tax;
      printf("NET PAY:\n", Netpay);
      break;
    default:
      printf("Invalid");
  }
  getch();
}
4

3 回答 3

3

该程序实际计算该值但不打印它。改变,

scanf("%d",&Hoursworked);

Grosspay=Hoursworked*150;
printf("Gross: \n",Grosspay);
Tax=Grosspay*0.1f;
printf("TAX:\n",Tax);


Netpay=Grosspay-Tax;
printf("NET PAY: \n",Netpay);

scanf("%f",&Hoursworked); //Houseworked is declared float

Grosspay=Hoursworked*150;
printf("Gross: **%f**\n",Grosspay); //you have to specify what type to print
Tax=Grosspay*0.1f;
printf("TAX:**%f**\n",Tax);


Netpay=Grosspay-Tax;
printf("NET PAY: **%f**\n",Netpay);

相应地更改完整的代码,你应该得到结果..:)

于 2012-09-05T11:03:31.260 回答
2

它已计算但未打印,因为您%fprintf.

于 2012-09-05T11:00:30.887 回答
1
printf("Gross:\n",Grosspay);

您缺少该字符串中变量的占位符。当您使用 printf 输出变量值时,第一个字符串必须包含插入以下变量的占位符。

printf("Gross: %f\n",Grosspay);

%f 将替换为 Grosspay 的值。

于 2012-09-05T11:00:17.260 回答