Most likely x
(a double) is not exactly 1
, it is 0.9999999...
. Check its exact value by printing x==1.0
or x<1.0
and see what is really true. Or add more digits to the output:
cout << "and after adding 1 and truncating = " << setprecision( 15 ) << x+1 << endl;
The rounding to integer will throw away all digits after the comma, so 1.999999...
becomes 1
.