0

I'm having trouble getting the the modulo function to work in greedy using this code

int result = 0.0;

printf("SHOW ME WHAT YOU GOT: ");
float paid = GetFloat();

while (paid >= 0.25)
{
    result = (paid/0.25) - (paid % 0.25);
    paid = paid - 0.25;
}

while (paid >= 0.10 && paid < 0.25)
{
    result = (paid/0.10) - (paid % 0.10);
    paid = paid - 0.10;
}

The error says that 'invalid operands to binary expression ('double' and 'double'). How do I fix this?

2

Modulo only works with integers. You are attempting to apply it to a float.

If this answers your question, please click on the check to accept. Let's keep up on forum housekeeping. ;-)

6
  • Thank you, I declared "int n = paid;" and edited my code accordingly
    – K. Roe
    Oct 1 '15 at 2:14
  • while (paid >= 0.25 && n > 0.0) { result = (paid/0.25) - ( n % (1/4)); paid = paid - 0.25; }
    – K. Roe
    Oct 1 '15 at 2:15
  • And now it gives me an error saying "remainder by zero is undefined" with the fractions following the modulo character. Do you know what might be causing this? Thank you for your time!
    – K. Roe
    Oct 1 '15 at 2:17
  • yeah, not surprised. integer arithmetic: (1/4) = 0. n % 0 is undefined.
    – Cliff B
    Oct 1 '15 at 2:21
  • Also, I really can't make any sense out of your logic. You divide the current paid by 25 cents (not counting remainder) and subtract the remainder from that. Say that you get the code working and start with $1.15. Result becomes (1.15 /.25 in whole numbers) - (remainder of .15) = 4 - 15 = -11. What does that do for you???? You need to count the whole number of coins and track the remainder, but these two operations are done separately!
    – Cliff B
    Oct 1 '15 at 2:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .