0

I am having trouble with my code for pset1/cash. When I check the code, this is my output:

:) cash.c exists

:) cash.c compiles

:( input of 0.41 yields output of 4' expected "4\n", not "0\n"

:( input of 0.01 yields output of 1 expected "1\n", not "0\n"

:( input of 0.15 yields output of 2 expected "2\n", not "0\n"

:( input of 1.6 yields output of 7 expected "7\n", not "1\n"

:( input of 23 yields output of 92 expected "92\n", not "5\n"

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

and everything else is correct.

Here is my code:

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

int main(void)

{
   
    
    float change;
    int dollars;
    do
    {
        change = get_float("Please enter your amount: ");
        
        dollars = round(change * 100);
        
    }
    while (change <= 0);
    
    int coins = 0;
    
    //quarters
    while (change >= 25)
    {
        coins++;
        change = change - 25;
    }
    while (change >= 10)
    {
        coins++;
        change = change - 10;
    }
    while (change >= 5)
    {
        coins++;
        change = change - 5;
    }
    while (change >= 1)
    {
        coins++;
        change = change - 1;
    }
    printf("%i\n", coins);
} 
0

Hmmm....

Should this code be doing calculations on dollars or change?

2
  • Wouldn't you want to do the round() function outside of your do-while loop? You'll only want to round positive numbers. I'd also recommend printf() right after rounding, to double check that this intermediate input is correct. Also looks like your 'change' variable remains a float (instead of an int) when you start your while function. – milar5 Aug 27 '20 at 8:37
  • dollars = round(change * 100); should not be in the do-while loop. And you seem to have swapped your variables. – EddyBeor Dec 9 '20 at 21:25

You must log in to answer this question.

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