cs50 greedy algorithm description
This is my code for the cs50 greedy algorithm. However, it doesn't output any answer. When I change float dollars to int dollars it works, but only for integers greater or equal to 1. For numbers like 0.32, it returns 0. How do I get my code to work for floats? Any help is appreciated.
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void)
{
float dollars;
int quarter = 25;
int dime = 10;
int nickel = 5;
int penny = 1;
do {
dollars = get_float("Change owed: ");
} while(dollars <= 0);
//convert dollars to cents
int cents = round(dollars * 100);
int coins = 0;
while(cents >= quarter)
{
cents -= quarter; //cents = cents - 25;
coins++;
}
while(cents >= dime) {
cents -= dime;
coins++;
}
while(cents >= nickel)
{
cents -= nickel;
coins++;
}
while(cents >= penny)
{
cents -= penny;
coins++;
}
printf("%i\n", coins);
}