我正在尝试编写一个程序来计算更改,但是它似乎不起作用。
我认为问题是欠1 /已付1;当我尝试在那打印值时,我什么也没得到(0)。
有什么帮助吗?

#include <stdio.h>

int main()
{
double owed, paid;
int  dollars, quarters, dimes, nickels, cents, remainder, owed1, paid1;
printf("how much did the customer have to pay ?\n");
scanf("%f",&owed);
printf("how much did the customer pay ?\n");
scanf("%f",&paid);
owed1 = owed * 100;
paid1 = paid * 100;
int change = paid1 - owed1;
dollars =  change / 100;
remainder = change % 100;
quarters = remainder / 25;
remainder = remainder % 25;
dimes = remainder / 10;
remainder = remainder % 10;
nickels = remainder / 5;
remainder = remainder % 5;
cents = remainder;
printf("%d",dollars);
printf("Dollars:%d, Quarters:%d, Dimes:%d, Nickels:%d, Cents:%d", dollars , quarters , dimes , nickels , cents );
return 0;
}

最佳答案

您在scanf中使用%f,这是浮点数的格式说明符,但变量为双精度。您应该使用%lf代替:

scanf("%lf",&owed);


paid也是一样。您应该从编译器那里得到警告。

关于c - C程序计算变化,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40444747/

10-12 14:48