我正试图完成哈佛大学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);
}
最佳答案
需要为函数参数指定类型:
changeF(float change, float div)
我不确定您是否真的希望这个函数的返回是int还是float!