1

I'm having some trouble de-bugging my code for greedy.c of CS50 Pset1.

When I input 4.2, I get 12 instead of 18. When I input 0.2, I get 2 (the correct answer). When I input 4, I get 10 (so here is the problem).

I'm not sure how to solve this one! Any help is appreciated. Thanks!

Here is my code:

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

    //get change
    int main(void)
    {

        float c;
        int count = 0;
            do
            {
                printf("How much change do you have to give back in $?\n");
                c = get_float();
            }
            while (c < 0);

        int change = round(c*100);


    // get number of coins

        while (change >= 25)
            {
                count++;
                change -=25;
            }
        while (change >= 10)
            {
                count++;
                change -=10;
            }
        while (change >= 5)
            {
                count++;
                change -=5;
            }
        while (change >= 1)
            {
                count++;
                change -=1;
            }

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

        return 0;

}
2
printf("%x\n", count);

prints count in hexadecimal. 0x12 is 18 in decimal. Use %i or %d instead of %x.

1
  • Thank you so much - I've been trying for hours! – pab_cs50 Oct 19 '17 at 16:27

You must log in to answer this question.

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