计算每周工资和加班工资的C程序不起作用?调试

时间:2017-03-26 23:32:17

标签: c debugging

我正在创建一个计算每周工资的计划,然后任何加班工资是该周正常工资的1.5倍。 我的代码如下:

#include <stdio.h>

int main()
{

  double payrate;
  double hours;
  double weeklypay = hours * payrate;
  double overtimehours = hours - 40;
  double overtimepay = weeklypay * 1.5;
  double overtimesalary =  weeklypay + (overtimehours * overtimepay);

  printf("What is your standard hourly pay rate?\n");
  scanf("%d",&payrate);
  printf("How many hours do you work in a week?\n");
  scanf("%d",&hours);

  if (hours <= 40)
    printf("This means your weekly pay is %d . \n", weeklypay);

  else
    printf("This means your weekly pay is %d . \n", overtimesalary);

  return 0;
}

然而,该计划仍然无法运作,我不明白为什么?而不是给我一个答案我总是得到每周薪水的答案为“1”。或者,如果我给出小数的答案,我会得到0的答案。像这样:

What is your standard hourly pay rate?
2
How many hours do you work in a week?
2
This means your weekly pay is 1 .

我在做什么,这是错的?我是C的新手,请原谅我的无知,但我可以真正使用一些帮助,因为我花了一整天的时间试图找出原因。

4 个答案:

答案 0 :(得分:0)

你的错误顺序有些陈述。您需要移动计算:

double weeklypay = hours * payrate;
double overtimehours = hours - 40;
double overtimepay = weeklypay * 1.5;
double overtimesalary =  weeklypay + (overtimehours * overtimepay);

在执行输入的代码之后关闭。否则你不会对任何有意义的数字进行操作。

此外,您的scanfprintf格式与您的变量类型不匹配。对%lf中的double变量scanf%f printf使用$timeout

最后,打开,阅读并注意您的编译器警告。所有这些都是通过任何合理的近期(阅读:最近20年)编译器清楚地报告的。

答案 1 :(得分:0)

使用%d时,您使用int格式说明符,该说明符需要double的地址,但您传递的是double的地址{1}}。使用错误的格式说明符会调用未定义的行为。

对于%lf,请使用scanf("%lf",&payrate); .... scanf("%lf",&hours); 格式说明符。

function stopSubmit(){
  return false;
}

window.onolad = function() {
  document.getElementById('f0').addEventListener('submit', stopSubmit, false);
}

您在中读取任何值之前,还要对值进行操作。您需要先读取值,然后然后对它们进行计算。

答案 2 :(得分:0)

查看我的评论

#include <stdio.h>

int main()
{

  double payrate;
  double hours;
  double weeklypay = hours * payrate; // Here payrate does not have a value nor does hours
  double overtimehours = hours - 40; // Ditto with hours 
  double overtimepay = weeklypay * 1.5; // From above this is not defined
  double overtimesalary =  weeklypay + (overtimehours * overtimepay); // ...

  printf("What is your standard hourly pay rate?\n");
  scanf("%d",&payrate); // Check the return value from scanf
  printf("How many hours do you work in a week?\n");
  scanf("%d",&hours); // See above

 // Here you are on your own...
  if (hours <= 40)
    printf("This means your weekly pay is %d . \n", weeklypay);

  else
    printf("This means your weekly pay is %d . \n", overtimesalary);

  return 0;
}

答案 3 :(得分:0)

#include <stdio.h>

int main()
{
int payrate;
int hours;
printf("What is your standard hourly pay rate? \n");
scanf("%d",& payrate);
printf("How many hours do you work in a week? \n");
scanf("%d",& hours);

int weeklypay = hours * payrate;
int overtimehours = hours - 40;
int overtimepay = weeklypay * 1.5;
int overtimesalary =  weeklypay + (overtimehours * overtimepay);


if (hours <= 40)
printf("This means your weekly pay is %d . \n", weeklypay);

else
printf("This means your weekly pay is %d . \n", overtimesalary);

return 0;
}