Online IDE's input vs local input problem

I participated in the October Cook Off and came up with the correct solution to the third problem, My solution: CodeChef: Practical coding for everyone . It gives wrong answer on online judges but run perfectly fine on my machine.

If I make a small change. Replacing scanf by cin then the judge identifies the solution as correct as can be seen in this submission: CodeChef: Practical coding for everyone

Changes:

In original :-
scanf(β€œ%d”, &T);

               cin >> str;

In modified:-
cin >> T;

                cin >> str;

What things can I do to avoid this in the future ? (Should I not mix and match scanf/printf with cin/cout ?) Aren’t online judges at fault here ? ( ideone and Codechef both are giving incorrect results here)

When replacing scanf fixes the error then this obvious that the issue is somewhere in the input-output aprt of your code, and it indeed is.

Let me ask, why are you using this-

 ios::sync_with_stdio(0);
  cin.tie(0);

Have you understood its function and working? Look bro, not knowing things about your language will always cost you somewhere or other, so dont ignore this part.

When you use these 2 lines, then you should either use scanf/printf throughout or cin/cout throughout, else it will malfunction.

Read about it here : Fast I/O for Competitive Programming - GeeksforGeeks

2 Likes