I am still having trouble understanding what I'm doing wrong. I know the key has to do with how I use the round function and how I convert in order to avoid float imprecision (e.g. .01 is actually .00999 ...). I'm having hard time figuring out what to convert to what. Shall I start with an initialized int variable, ask for float user input (but how does int and float interact here??), and then use round() in order to convert it to type double which is more precise than keeping the user's "change" as float??
Here is my code:
int main(void) {
int change;
int quarters;
int dimes;
int nickles;
int pennies;
int coins = 0;
do
{
printf("how much change?");
//change = GetFloat();
change = round(GetFloat()) * 100;
// change = change / 100;
printf("%d\n", change);
for (quarters = 0; change >= 0.25; quarters++)
{
change = change - 0.25;
printf("%d\n", change);
}
coins = quarters + coins;
for (dimes = 0; change >=0.10; dimes++)
{
change = change - 0.10;
printf("%d\n", change);
}
coins = dimes + coins;
for (nickles = 0; change >= 0.05; nickles++)
{
change = change - 0.05;
printf("%d\n", change);
}
coins = nickles + coins;
for (pennies = 0; change >= 0.01; pennies++)
{
change = change - 0.01;
printf("%d\n", change);
}
coins = pennies + coins;
printf("%i\n", coins);
}
while (change < 0);
}