你好,我对编码的经验很少但是我在学校工作的问题要求我输入一些像#34; 12.5"并处理该输入以给我一定的输出。
我正在尝试修改此代码,但没有运气
代码在C
中例如" 费率"会像" 12.5 ",但它只会读取" 12 "然后用 12 继续计算,忽略 .5
我需要它来阅读整个 12.5
感谢您提供的任何帮助。
#include <stdio.h>
int main ()
{
int hours_2weeks;
int rate;
int Gross_Pay;
int Social_security_tax;
int Federal_tax;
int State_tax;
int Medical_dental;
int Net_Pay;
printf("\nPayroll Program for X Y Z Corp.\n");
{
printf("Enter hours worked: ");
scanf("%d", &hours_2weeks);
printf("Enter hourly rate: ");
scanf("%d", &rate);
}
printf("This employee worked %d hours with a salary of $%d per hour. Here is the salary statement for this employee:\n",hours_2weeks,rate);
Gross_Pay=hours_2weeks*rate;
printf("Gross Pay: %d\n", Gross_Pay);
Social_security_tax=hours_2weeks*rate*.06;
printf("Social security tax: -%d\n", Social_security_tax);
Federal_tax=hours_2weeks*rate*.1;
printf("Federal tax: -%d\n", Federal_tax);
State_tax=hours_2weeks*rate*.05;
printf("State tax: -%d\n", State_tax);
Medical_dental=hours_2weeks*rate*.05;
printf("Medical/dental: -%d\n", Medical_dental);
Net_Pay=hours_2weeks*rate-(hours_2weeks*rate*.06)-(hours_2weeks*rate*.1)-(hours_2weeks*rate*.05)-(hours_2weeks*rate*.05);
printf("Net Pay: %d\n", Net_Pay);
}
答案 0 :(得分:3)
您将所有输入变量定义为Integer。即使你键入一个double,系统也会将这些值转换/转换为整数......
你这样做:
int hours_2weeks;
printf("Enter hours worked: ");
scanf("%d", &hours_2weeks);
改为:
double hours_2weeks;
printf("Enter hours worked: ");
scanf("%lf", &hours_2weeks);
答案 1 :(得分:2)
rate
是一个整数变量。将其更改为float
,并将scanf
调用更改为
scanf("%f", &rate);
答案 2 :(得分:2)
当您需要小数点时,您必须将数据类型更改为float
或double
,当您这样做时,必须将格式说明符更改为%f
float
1}}和%lf
double
。
答案 3 :(得分:2)
你已声明费率为:int rate;
,它的作用是,现在的费率变量只能存储积分值(如1,4,66,3333,虽然它有一个上限)而不是浮点数数字(如3.55,4.2112,34.533,带小数的数字)。
为了能够存储浮动值(带小数的数字),您需要将速率指定为:float rate;
。
另外,为了将浮点值打印到控制台输出,您需要将%f
%d
答案 4 :(得分:0)
现在感谢它完成了我需要它做的事情。
#include <stdio.h>
int main()
{
float hours_2weeks;
float rate;
float Gross_Pay;
float Social_security_tax;
float Federal_tax;
float State_tax;
float Medical_dental;
float Net_Pay;
//printf("\nPayroll Program for X Y Z Corp.\n");
{
printf("Enter hours worked: ");
scanf("%f", &hours_2weeks);
printf("Enter hourly rate: ");
scanf("%f", &rate);
}
printf("This employee worked %d hours with a salary of $%d per hour. Here is the salary statement for this employee:\n",hours_2weeks,rate);
Gross_Pay=hours_2weeks*rate;
printf("Gross Pay: %.2f\n", Gross_Pay);
Social_security_tax=hours_2weeks*rate*.06;
printf("Social security tax: -%.2f\n", Social_security_tax);
Federal_tax=hours_2weeks*rate*.1;
printf("Federal tax: -%.2f\n", Federal_tax);
State_tax=hours_2weeks*rate*.05;
printf("State tax: -%.2f\n", State_tax);
Medical_dental=hours_2weeks*rate*.05;
printf("Medical/dental: -%.2f\n", Medical_dental);
Net_Pay=hours_2weeks*rate-(hours_2weeks*rate*.06)-(hours_2weeks*rate*.1)-(hours_2weeks*rate*.05)-(hours_2weeks*rate*.05);
printf("Net Pay: %.2f\n", Net_Pay);
}