2016-11-06 61 views
-2

我想编写一个程序来计算变化,但它似乎并没有工作。 我认为问题是欠1 /付1;当我试图在那里打印值时,我什么也没有(0)。 有什么帮助吗?C程序来计算变化

#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; 
} 
+2

解决此类问题的正确工具是您的调试器。在*堆栈溢出问题之前,您应该逐行执行您的代码。如需更多帮助,请阅读[如何调试小程序(由Eric Lippert撰写)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。至少,您应该\编辑您的问题,以包含一个[最小,完整和可验证](http://stackoverflow.com/help/mcve)示例,该示例再现了您的问题,以及您在调试器。 –

回答

0

你在你的scanf函数,这是一个浮动的格式说明使用%f,但你的变量是双打。您应该使用%lf代替:

scanf("%lf",&owed); 

同样的事情paid。你应该从编译器那里得到警告。