2016-12-12 111 views
1

這不是我的整個代碼,但我不斷收到此錯誤:無效的操作數到二進制表達式無效的操作數到二進制表達式('double'和'double')

printf("How much change is owed?\n"); 
float change= GetFloat(); 
float roundf(float change); 
change*=100; 
int rem; 

while (change>0) 

{ 
    if(change>=0.25) 
    rem=change % 0.25;  > error, saying that this is a double???? 
} 
printf ("%d\n", rem);   I need the modulo , it is not working 
return 0; 
+0

改爲使用fmod(a,b)。 – dasblinkenlight

+0

乘以100並舍入後,您應該可以使用整數數學和整數變量來編寫程序的其餘部分。 – user3386109

+0

嗯,如果'變化> 0.0',則'while(變化> 0) { 如果(變化> = 0.25) rem = 「看起來像一個無限循環。 – chux

回答

2

在C和C++中,運算符%未定義爲浮點數。它僅針對整數類型定義。

因此,編譯器,因爲在此表達

rem=change % 0.25; 

兩個操作數是浮點數發出錯誤。這裏0.25double類型的浮點文字,變量change被聲明爲具有類型float。從<math.h>

float change= GetFloat(); 

使用fmodremainder功能。

相關問題