### Geting an interger to float

I dont know how to get the numbers to show up on the side but this is my program for counting coins and this is the error i keep getting

[Warning] converting to `int' from `float' on line 20(N2=N1*100)

How would I fix this issue? The program works sometimes but there are rare occasions where it will round a number down (like i would take 167 and make it it 166) and it is throwing off the rest of the calculations.

Last edited on
`int float N2, N3, N4, N5, N6, N7, N8, N9;`
Last edited on
that gives me errors with the remainders
try a `double` instead?

also,
 ``123456`` ``````if(your code is formatted like this) { it is easier to read }``````

try using the code function that these forums have. It is very helpful for getting results.
Last edited on
Pleas use the code tags - the <> button on the right.

You are getting this error because N2 is an int on the left side of the assignment, so it has to convert whatever the answer is (of type float)to an int.

However you need ints to do the remainder calcs.

So the answer is to use ints where needed, then cast them to double once the remainders have been calced.

`MyDouble = static_cast<double>(MyInt)`

HTH
`N2 = (float)N1 * 100;`
@Stewbond (1427)

Hang on ..... N1 is already a float, N2 is an int - so that is the original problem.

The OP could do C casting with an int:

`N2 = (int)N1 * 100;`

This is OK logically because the multiplication has converted to cents already - the explicit cast should remove the warning.

I think if the OP names the variables better there might not be as much confusion - there are logical errors in which variables are being used for which calculation.

HTH

Edit:

I mentioned the static_cast because that seems to be the C++ way of doing things.
Last edited on
@TheJJJunk , he(kmcbeth) can't use all variable as float because float aren't work with % operator...
 (int)N1 * 100
This will drop the fractional part.
`int(N1 * 100)` should be used instead.
Thank you guys/girls. You are all awesome. I got rid of the error thanks to helios!
Topic archived. No new replies allowed.