- Joined
- 12/3/09
- Messages
- 36
- Points
- 18
The assignment is fine. It's the comparison in the if statement that's the issue. You have a/3 == b. The value of b will be converted to a double, which is fine since it's not very large. However, it's not always safe to assume that a whole number stored as a double will give you the number you expect. For example, try this code:
Code:#include <iostream> using namespace std; int main () { int i; double j = 0; for (i = 0; i < 10; i++) { j += 0.1; } cout << (j == 1) << endl; return 0; }
It will output 0.
This is interesting, I tried to output j in in your code, it shows "1" but however it denies to say j equals integer "1".
Why is this? Need to dig in my text books.
Thanks.