float f = 122/11;You'd expect something like a value of 11.0909090909. In fact the value is 11. Why is this? because the expression on the right hand side (known as an rvalue) is integer/integer. So it uses integer arithmetic which throws away the fractional part and assigns 11 to f. Changing it to
float f = 122.0/11will correct it. It's a very easy gotcha.
Types Bool and Int
In C, there is no such type as a bool. Expressions in C were based on a zero being false or a non zero being true. In C++ the type bool can take the values true or false. These values are still equivalent to 0 and 1. Somewhere in the compiler it will have aconst int false=0;Or at least it acts that way! The two lines below are valid without casting so behind the scenes, bools are implicitly converted to ints and can even be incremented or decremented though this is very bad practice.
const int true= 1;
bool fred=0;
int v = true;
Look at this code
bool bad = true;The if will still do the if as the bad variable is non zero but it is bad code and should be avoided. Good practice is to use them as they are intended. if (!v) is valid C++ but I prefer the more explicit if (v != 0). That however is a matter of taste, not a must-do directive.
bad++
if (bad) ...
On the Next Page - Learn to use Enums

