I've written a code which works for both c++ and Xcode.
When executed there are no errors.
The only problem is I get different answers. Does anyone know why this is happening?
You are probably relying on undefined or implementation defined behavior.
There are many things that could cause this but make sure you initialize variables before using them, make sure you don't read and write outside the bounds of arrays, make sure you don't dereference invalid pointers, etc.
If you can't find the problem yourself you could post it here (if it's not too big) and we could have a look at it.