0

can someone tell me why my code doesn't output properly?

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

int main(void)
{
    int dollars;
    do
    {
        dollars = get_float("Change owed: ");
    }
    while (dollars < 0);
    
    int cents = round(dollars * 100);
    
    
    int coins = 0;
    while (cents >= 25)
    {
        cents -= 25;
        coins++;
    }
    
    while (cents >= 10)
    {
        cents -= 10;
        coins++;
    }
    while (cents >= 5)
    {
        cents -= 5;
        coins++;
    }
    
    while (cents >= 1)
    {
        cents -= 1;
        coins++;
    }
    printf("%i\n", coins);
    
}
1
  • 1
    Please edit your answer to include the output you expect and what you are getting. Jul 30 '21 at 14:56
2

The problem is that you're trying to store the float the get_float function gets from the user in a variable that you have initialized as an integer. That's truncating the float to the integer part and discarting the rest. You have to change the data type of that variable to a float. I hope this actually helps you.

You must log in to answer this question.

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