Using cin and scanf in same code gives wrong output

I have used both cin and scanf for taking input in my c++ code, and then I see its showing unexpected results, whys that so? here’s the code:

*int n; cin >> n;
for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
for (int i = 0; i < n; i++) cout << a[i] << " ";*

input: 5 1 2 3 4 5 output: 4201072 6422224 6422280 6422476 1993395392

Are you using fast I/O?


The whole point of ios::sync_with_stdio(false) is that it unties it from scanf and other input methods. So you could be reading integers in weird order (and seem to be getting garbage values). A bit more on that here.


thank u sir!

Also you have not declared the array before.I think that is why you got wrong answer.Basically you can use cin or scanf no problem .cin is slower than scanf that is only the difference.

The code as presented doesn’t even compile, so it’s clearly not representative of his actual code. I’d be wary of drawing too many conclusions from it :slight_smile:

You can use cin or scanf “no problem”. You can even use both in the same program, provided you keep them in sync. But you can’t use them both ios::sync_with_stdio(false), or you will almost certainly get WA, as @galencolin said :slight_smile:

1 Like

Thanks, I have only shown just a part of my code, that’s my bad because I did not know the fast io has any other function other than just fast io.

1 Like

It’s always a good idea to post the full code (or, best of all, a link to the Submission) precisely because of these “unknown unknowns” :slight_smile: