So I've made this program with modulus and it made sense to me, but when I ran it I got an error message saying I can't use mod on floats.

Since then I've been trying to replace this:

change = change%0.25;

With this:

change -= (change/0.25)*0.25;

With no success.

Instead of mimicking mod, it just resets change to 0. I'm very new to this, and I'm trying my best, this is just very confusing for me.

This is the full program right now:

int main(void)
int count = 0;
int coins=0;
float change;
change = get_float("Change Owed?");

        count += change/0.25;
        change -= (change/0.25)*0.25;
        count += change/0.10;
        change -= (change/0.10)*0.10;
        count += change/0.05;
        change -= (change/0.05)*0.05;
        count = count + (change/0.01);
        change -= (change/0.01)*0.01;

printf("%d\n", count);

I used the printf line to check change's value after the first if, and it's 0.000

It's not even a complicated program, only 40 lines and still for some reason I can't figure out what's wrong, any help appreciated. Thanks


Please have another look at the second hint of this exercise (https://docs.cs50.net/2018/x/psets/1/cash/cash.html#hints).

After you have followed that advise, you will be able to use the modulo operator like you planned ...

And to quote Cliff B:

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

  • So just to make sure, I need to change the 0.25, 0.10 0.05 0.01 to 25, 10, 5, 1 and then at the end change them back to a float? Oct 11 '18 at 13:36
  • Actually you don't have to change them back. Once you have converted the input to cents, you can keep doing everything with integers. Oct 11 '18 at 14:10

You must log in to answer this question.

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