Two identical codes when launched on two different computers give two correct answers that look different. How can this be explained? The goal of the program is to calculate А + В.Input: 1,000,000,000,000,000,000. Output-1: 2,000,000,000. Output-2: 2e + 009

#include <iostream> #include <fstream> using namespace std; int main(){ ifstream cin("input.txt"); ofstream cout("output.txt"); int a, b; cin » a » b; cout « a + b; return 0; } 

I tried the code:

 #include <iostream> int main() { std::cout << (int)2000000000; return 0; } 

It is displayed as 2000000000.

Now I tried again to open the same code in another document, now it is displayed as 2000000000. What is unclear about this, but now everything is fine, thanks to everyone for their help.

  • 2
    You are probably very surprised, but 2,000,000,000 and 2e + 009 are the same number. 2e + 009 = 2 * 10 ^ 9. Just you locale settings on computers are different. - Donil
  • Read about [formatted input / output] [1] [1]: cppstudio.com/post/319 - Donil
  • @Alexander Dimov, what OS, compiler, etc.? Or compiled in one place, and run in another? - avp
  • @avp On one Win7 ultimate and GCC 4.7, on the second Win7 Enterprise and GCC 3.4.5 / - Xas
  • 2
    @Alexander Dimov: then, perhaps, the problem is that the project is not compiled on time. Try to manually delete the executable file on the old project and run it again (so that the file is guaranteed to be “fresh”). - VladD

1 answer 1

Learn the formatted cout output . In this case, you need the fixed parameter: cout.setf(ios::fixed);