我正在嘗試爲哈佛大學完成CS50x的習題集1,並且在我的程序中遇到過一種錯誤。它向我拋出一個浮點異常錯誤;我已經研究了這一點,它似乎主要是當你試圖除以零時引起的。現在,當我查看我的代碼時,我似乎無法找到一個變量甚至等於零的地方,更不用說被零除。無法解決浮點異常
#include <stdio.h>
#include <cs50.h>
int changeF(change, div)
{
int c = change/div;
return c;
}
int main(void)
{
printf("Enter the bill amount:");
float bill = GetFloat();
printf("Enter the payment amount:");
float payment = GetFloat();
float q = .25; float di = .1; float n = .05; float p = .01;
float change = payment - bill;
do
{
if (bill >= 0 && payment >= 0 && change >= 0)
{
if (change >= q)
{
float quarters = changeF(change, q);
printf("%f quarters", quarters);
change = change - (.25*quarters);
}
if (change >= di)
{
float dimes = changeF(change, di);
printf("%f dimes", dimes);
change = change - (.1*dimes);
}
if (change >= n)
{
float nickels = changeF(change, n);
printf("%f nickels", nickels);
change = change - (.05*nickels);
}
if (change >= p)
{
float pennies = changeF(change, p);
printf("%f pennies", pennies);
change = change - (.01*pennies);
}
}
}
while (change > 0);
}