2016-11-06 63 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。你應該從編譯器那裏得到警告。