Lucky500 Lucky500 - 1 year ago 212
C Question

Error: invalid operands to binary expression ('float' and 'float')

I apologize if this question has been asked before. I looked around and was not able to find a solution, I am new to C.
I understand that I am not able to get a % from a float. How would I be able to capture the remainder of this math, if I am using 2 floats?

#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*/
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);

Answer Source

You may want to check fmod.

You can also do something like change = change - (int)(change / quarter) * quarter

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download