c++ – The code gives two correct, but different answers on different computers

Question:

Two identical codes, when run on two different computers, produce two correct answers that look different. How can this be explained? The goal of the program is to calculate A + B. Input: 1000000000 1000000000. Output-1: 2000000000. 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;
}

Tried the code:

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

It is displayed as 2,000,000,000.

Now I tried to open the same code again in another document, now it is displayed as 2,000,000,000. What is the reason for this is not clear, but now everything is fine, thanks to everyone for the help.

Answer:

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

Scroll to Top