0

Hello for this program that is meant to return the number of coins needed to give the change it works fine until the value goes between 0 and 0.05. When I input 0.01, the program does not go into the while loop at all (I checked by making something print at the start of the while loop). Why does it not run?

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

    int main(void)
    {
        float change;
        do {
            change = get_float("Change owed: ");
        }
        while (change < 0);
        int no = 0;
        while (change >= 0.01) {
            if (change >=  0.25) {
                change = change - 0.25;
                no = no + 1;
                printf("a %f\n", change);
            }
            else if (change >= 0.1) {
                change = change - 0.1;
                no = no + 1;
               printf("b %f\n", change);
            }
            else if (change >= 0.05) {
                change = change - 0.05;
                no = no + 1;
                printf("c %f\n", change);
            }
            else if (change >= 0.01) {
                change = change - 0.01;
                no = no + 1;
                printf("c %f\n", change);
            }
        };
        printf("%i", no);
    }
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
dng
  • 1,275
  • 1
  • 8
  • 10
  • 1
    If you [learn how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) you will probably see that the value is actually not exactly equal to `0.01`, but a little lower. See the linked duplicate for more information. – Some programmer dude Feb 05 '18 at 07:44
  • So do I need to round every value? – dng Feb 05 '18 at 07:46
  • 2
    After getting the value from the user, multiply by 100, and convert to an integer using the `round` function. Then write all of the rest of the code using integer math. – user3386109 Feb 05 '18 at 07:50
  • 2
    How does the title *Why won't the program run into the 0.01 section* explain anything about the question in anyway? – Ajay Brahmakshatriya Feb 05 '18 at 07:58
  • 1
    @Someprogrammerdude: What at that page you link to would lead a person to realizing a value the computer printed as “0.01” was not 0.01? – Eric Postpischil Feb 05 '18 at 13:41

0 Answers0