1

Desperately need help for pset 1 cash!

My code failed 2 of the check50 tests: :( input of 23 yields output of 92 :( input of 4.2 yields output of 18

I believe it's a rounding problem, but I've been trying and retrying and searching but I still can't find a solution.

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

int main(void)
{
    float c;
    int t, n=0;

    do
    {
        c = get_float("Change owed: ");
    }
    while (c < 0);

    //Round and convert
    c *=100;
    t =round(c);

    //quarters
    while (t >= 25)
    {
        n++;
        t -=25;
    }

    //dimes
    while (t >= 10)
    {
        n++;
        t -=10;
    }

    //nickels
    while (t >= 5)
    {
        n++;
        t -=5;
    }

    //pennies
    while (t>=1)
    {
        n++;
        t -=1;
    }

    //prints coins used
    printf("%o\n", n);
}

1 Answer 1

3

Try looking at the format specifier you're using to print the number of coins used.

Hint: It shouldn't be %o, that one is used for octal values (base-8).

2
  • Thank you so much! I thought it was a rounding thing, so I just blindly focused on that portion of the code. Is there a list available somewhere which shows me what kind of placeholders I should avoid?
    – spearmint
    Commented Jul 22, 2018 at 5:28
  • @spearmint It's not so much about which ones you should avoid but using the correct one at the correct place. You should cross-check from somewhere like here but these, more or less, become second nature over time. In CS50, you'll mainly be using %c, %i/%d, %f and %s for chars, integers, floating-point values and strings respectively.
    – ziayanj
    Commented Jul 22, 2018 at 6:17

You must log in to answer this question.

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