rel1x
rel1x

Reputation: 2441

Wrong output or read from stdin, C++

My program:

int main() {  
    int t;
    cin >> t;
    while (t--) {
        int a, b;
        cin >> a >> b;

        printf("%i, %i\n", a, b);
    }   

    return 0;
}

Why my output is:

551124992, 551129087
2147483647, 551129087
2147483647, 551129087
...

if input is:

81
551124992 551129087
2205057024 2205061119
4204900352 4204904447
2341777408 2341781503

I expect to see a different response:

551124992, 551129087
2205057024, 2205061119
4204900352, 4204904447
...

But if my input is

2
12 15 
2 3 

I have a right output:

12, 15 
2, 3 

I don't understand, what's wrong. Here is a demo: https://ideone.com/izmRCn

Upvotes: 1

Views: 50

Answers (2)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727047

You are using long long in your demo, but the format specifier for it is invalid. It should be %lli, not simply %i:

// This is copied from your demo:
while (t--) {
    long long a, b;
    cin >> a >> b;
    // This is modified
    printf("%lli, %lli\n", a, b);
    //       ^^    ^^
}

Demo (forked from your code).

Upvotes: 1

These numbers are too large to fit in an int (assuming 32-bit ints, as on Windows):

81
551124992 551129087
2205057024 -> 2205061119 <-
4204900352 -> 4204904447 <-
2341777408 -> 2341781503 <-

The maximum value that can be stored in a 32-bit int is 2147483647.

Use long long.

Upvotes: 4

Related Questions