I have a piece of code here that gives the wrong answer
The problem was that the result would always be an integer. I cannot understand why this is the case? the variable 'result' is declared as floating point, so why does the integer value 'a' have any affect here? Do most compilers do this or is it only g++Code:int a = 5; float result; float b = 10; result = a / b;
I managed to fix my code by type casting and adding (float) in front of the division. Is there a better, more elegant way of doing this?
Many thanks,Code:int a = 5; float result; float b = 10; result = (float) a / b;