# Still Need Help. Pset1 Geedy with round()

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);
``````

}

It looks like you have a couple of misconceptions in your code. Mostly, you need to understand how integer math works compared to normal math. For instance, when the content of a float is stored in an int, the fractional part of the number is truncated, not rounded. Further, consider what happens when you subtract fractional numbers from an int. Try running the following code:

``````#include <stdio.h>

int main(void)
{
int c = 2;
c = c - 0.25;
printf("2 - 0.25 = %i", c );
}
``````

THe result is `2 - 0.25 = 1`

Next, the round() function rounds a number at the decimal point. That means 4.19999 will be rounded to 4.0, not 4.19 or 4.20. Now look at this line of code:

``````change = round(GetFloat()) * 100;
``````

This will round to the nearest dollar before multiplying by 100. Since you want to convert dollars and fractions of dollars (cents) to an integer of cents, you need to multiply by 100, thus putting the decimal at the correct spot, and then round the result before storing in an int.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

• This was much more helpful, and led to my understanding and completion of the Pset1 Problem Sets. Thanks you :) Sep 14, 2016 at 19:05