0

Can anyone explain to me why this is the result im getting?

Checking........

:( input of 4.2 yields output of 18 expected "18\n", not "22\n"

everything else is correct.

this is my code

    #include<stdio.h>
#include<cs50.h>
int main(void)
{
    float a; //change
    int b;  // int
    int c;  //25
    int d;  //10
    int e;  //5
    int f; //1
    int g; // amount of quarters
    int h; // amount of coins
    int j;// amount of dimes
    int k; //amount of nickles
    int l; // amount in penny
do  {
    a = get_float("How much change is owed? ");
    }
while(a < 0);
        //converting to int
    {   b = a * 100;
        //cal the remainder of $ less 25
        c = b % 25;
        //cal the amount of quarters
        g = b / 25;
        //remainder of $ in 10
        d = c % 10;
        //cal the amount of dimes
        j = c / 10;
        //cal the $ in 5
        e = d % 5;
        // amount of coins
        k = d / 5;
        //penny
        f = e % 1;
        l = e / 1;
    }
    //total amount of coins
    h = g + j + k + l;
    printf("%i\n", h);

}
1

This is the key lesson of this pset, so here's a pop quiz. How many coins in $4.19? How precisely are floats stored? What happens to the fractional part of a float when it is cast or converted to an int? Is it rounded or truncated?

Also, you should really get in the habit of using descriptive names for variables. Using single letter variable names is really confusing for anyone (including the author) trying to debug a program and, except as for loop counters, is a really bad practice.

If this answers your question (well, pushes you in the right direction), please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

3
  • I'm having the exact same issue with 4.2 = 22, any further clarification would be helpful in regards to float/int conversion. Since we're multiplying the input immediately by 100 to get the total cents I'm not sure how rounding affects this. Nov 13 '17 at 18:33
  • Thanks for the hint Cliff! Right on point and got it running properly. Nov 13 '17 at 20:07
  • Glad you worked it out. Could you please accept the answer? Otherwise it'll sit in the unanswered question pool forever. thanks.
    – Cliff B
    Nov 13 '17 at 21:13

You must log in to answer this question.

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