错误:二进制表达式的操作数无效('float'和'float')

Luc*_*500 1 c floating-point module cs50

如果以前曾问过这个问题,我道歉.我环顾四周,无法找到解决方案,我是C的新手.我明白我无法从浮动中获得%.如果我使用2个浮点数,我怎么能捕获这个数学的剩余部分?

#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);
}
Run Code Online (Sandbox Code Playgroud)

小智 6

你可能想检查 fmod.

你也可以这样做 change = change - (int)(change / quarter) * quarter