-2

could someone please have a look at the following code and tell me what I'm doing wrong? I'm attaching the check50 checks. Thanks so much in advance

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

int main(void)
{

    float change;
    do
    {
        printf("O hai! How much change is owed?\n");
        change = get_float();
    }
    while (change < 0 );


    float cents = change *100;   // create float cents

    // initialise counter with 0
    int counter = 0;

    // loop as long as cents  bigger than 0
    while (cents > 0)
    {

        // substract 25 cents each time
        while (cents >= 25)
        {
            cents = cents - 25;
            counter++;
        }
         // substract 10 cents each time
        while (cents >= 10)
        {
            cents = cents - 10;
            counter++;
        }
         // substract 5 cents each time
        while (cents >= 5 )
        {
            cents = cents - 5;
            counter++;
        }
         // substract 1 cent each time
         while (cents >= 1 )
        {
            cents = cents - 1;
            counter++;
        }

    }

    printf("%i\n", counter);
}
Checking..........

:) greedy exists

:) greedy compiles

:) input of 0.41 yields output of 4

:) input of 0.01 yields output of 1

:( input of 0.15 yields output of 2 did not find "2\n"

:) input of 1.6 yields output of 7

:) input of 23 yields output of 92

:( input of 4.2 yields output of 18 timed out while waiting for program to exit

:) rejects a negative input like -.1

:) rejects a non-numeric input of "foo"

:) rejects a non-numeric input of ""

jstrnbrg
  • 17
  • 2
  • 3
    Now is the perfect time to [learn how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). Problems like yours, where you even know the input, is best solved through debugging. And stackoverflow.com is not a debugging-service. Please read [the help pages](http://stackoverflow.com/help), especially ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). – Some programmer dude Jan 02 '18 at 12:40
  • `float cents = change *100;` --> `float cents = round(change *100);` and /or change `while (cents > 0)` --> `while (cents >= 1)` – chux - Reinstate Monica Jan 02 '18 at 12:43
  • 1
    There might be a problem with floating-point where,for example, 0.1 can't be represented exactly and multiplying by 100 could yield a number that fails the condition `(cents >= 10)`. Make `cents` an int and round when converting from floating-point. – M Oehm Jan 02 '18 at 12:44
  • If you use floating point, read http://floating-point-gui.de/ – Basile Starynkevitch Jan 02 '18 at 12:47
  • Have you tried searching here: https://cs50.stackexchange.com/ ? – Bob__ Jan 02 '18 at 12:51
  • It's wrong to have `cents` as a `float`. – meaning-matters Jan 02 '18 at 12:52
  • It is not wrong to have cents of a `float`. Using an `int` would not solve the problem. OP is correctly scaling money to the base unit, just not dealings with impreciseness incurred at input. – chux - Reinstate Monica Jan 02 '18 at 12:54
  • @chux Meaning-wise it's wrong to represent an intrinsic integer property as a float. And meaning matters a lot in programming. Mistakes like this result in a lot of trouble. As is very often the case, getting a program correct meaning-wise prevents a lot of issue. The same is true here. – meaning-matters Jan 02 '18 at 13:03
  • Related: https://cs50.stackexchange.com/questions/27968/imprecision-problem-cs50-greedy – Bob__ Jan 02 '18 at 13:03
  • @meaning-matters Money is not an intrinsic integer. Sure for learner code of adding/subtraction coins, money as an `int` is fine. Yet for real computations involving cost per unit, interest, taxes, mortgage payments, etc. `int` falls short. Money involves more considerations than just type. [ – chux - Reinstate Monica Jan 02 '18 at 13:09
  • @chux Of course I was only talking about this situation. We're explicitly dealing with cents here (that need to be counted down). In this context, the most meaningful representation is an integer. Indeed, another context probably requires a different representation. – meaning-matters Jan 02 '18 at 13:57
  • @Someprogrammerdude thanks for that link. Bookmarked it! – jstrnbrg Jan 02 '18 at 15:47

1 Answers1

1

Do this:

int cents = round(change * 100);

Simply because there are no cent fragments in this context.

This also makes sure that wrong input of for example 7.324 (instead of 7.32) gets 'corrected'.

meaning-matters
  • 21,929
  • 10
  • 82
  • 142