gpt4 book ai didi

c - 错误 : invalid operands to binary expression ('float' and 'float' )

转载 作者:太空宇宙 更新时间:2023-11-04 00:29:04 25 4
gpt4 key购买 nike

如果之前有人问过这个问题,我深表歉意。我环顾四周,找不到解决方案,我是 C 的新手。我知道我无法从 float 中获得 %。如果我使用 2 个 float ,我将如何捕获这个数学的剩余部分?

#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>

/*
** Always use the largest coin possible
** keep track of coins used
** Print the final amount of coins
*/

int main (void)
{
float change;
int counter = 0;
int division;
//float rem;
float quarter = 0.25;
//float quarter = 0.25, dime = 0.10, nickel = 0.05, penny = 0.01;
/* Prompt user for an amont of change*/
do{
printf("How much do we owe you in change? ");
change = GetFloat();
}
while (change <= 0);
if (change >= quarter)
{
division = (change / quarter);
counter += division;
//change = (int)(change % quarter);
printf("change: %.2f\n", change);
printf("counter: %d\n ", counter);
}

return (0);
}

最佳答案

你可能想检查一下 fmod .

您还可以执行类似change = change - (int)(change/quarter) * quarter

关于c - 错误 : invalid operands to binary expression ('float' and 'float' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37455305/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com