Rolf Magnus already told you what you need to do, I just thought I'd
add a clarification point.
C and C++ just look at and evaluate one part of an expression at a
time. It evaluates that, then moves to the next operation. In the code:
int a, b;
float c = a/b;
the compiler starts by evaluating a/b. The only information it pays
attention to at this stage is what is in 'a/b'. It sees that a is an
int and b is an int, so it performs integer division on a and b. (This
gives 0 is a < b since it truncates -- rounds down -- any decimal
portion.) Only after getting this (integer) result does it convert it
to a floating point number and store it in c.
Rolf's fix, 'float c = static_cast<float>(a) / b', changes the
evaluation sequence. Now the compiler starts with the cast. So the
first step is to convert a to a float. Then it looks at the division.
Now the division is a float divided by an integer. In this case, the
C/C++ rules tell it to promote the integer to a float and do floating
point division, which is then stored in c. This gives the right answer.
(But again, I would reccomend taking Old Wolf's advice and using
double.)
One more, this time more complicated example:
int a, b, c;
int d = (a / b) / (static_cast<float>(c));
The sequence of evaulation is:
1. The compiler looks at 'a/b' and does integer division since both
operands are ints (call this intermediate result r1)
2. The compiler looks at 'static_cast<float>(c)' and performs the cast
(call this converted value r2)
3. The compiler looks at r1/r2. r1 is an int and r2 is a float, so it
converts r1 to a float and does floating point division. (call the
quotient r3)
4. Finally, the compiler looks at the assignment d = r3. d is an int,
so it demotes r3 to an integer and stores the value.
(Actually, as a technical note, I think the standard leaves the order
of steps 1 and 3 undefined, so the compiler could do it either way. I'm
not positive about that, but I think that's the way it is.)