1

Is there any way that I can print the output only after all the inputs are taken by the program? In other words is there anything equivalent to cin.tie(0) in Java?

In C++ when we include cin.tie(0) ,we untie std::cout and std::cin .By doing this we make sure that the output is not immediately flushed on to console after the input is given by the user,instead the output is flushed once the user is done giving all their inputs.

Consider the case where we have 3 test cases for a program as an input.Our program will output Hello World!.

Input

Test Case 1
Test Case 2
Test Case 3

Without using cin.tie(0): Program(c++)

int main(){
   int t;//holds the info of number of test cases
   cin>>t; 
   for(int i=0;i<t;i++){
      string s; 
      cin >>s; 
      cout<<"Hello World";
   }
   return 0;
}

Console:

Test Case 1
Hello World!
Test Case 2
Hello World!
Test Case 3
Hello World!

By using cin.tie(0):

int main(){
   ios_base::sync_with_stdio(false); 
   cin.tie(0);
   int t;//holds the info of number of test cases
   cin>>t; 
   for(int i=0;i<t;i++){
      string s; 
      cin >>s; 
      cout<<"Hello World";
   }
   return 0;
}
Test Case 1
Test Case 2
Test Case 3
Hello World!
Hello World!
Hello World!

Is there any way I can replicate cin.tie(0) in Java?

cigien
  • 50,328
  • 7
  • 37
  • 78
  • I am not aware of any built-in functionality for `tie` in Java. However, you could easily create such a class yourself as a simple wrapper. You just have to `flush()` before each input. So whenever you read input, call `flush()` on the output stream first. Thats it, you do not have to do anything else. The purpose is to prevent dialogs being printed in the wrong order like `___ Enter your name:` instead of `Enter your name: ___`. – Zabuzard Sep 18 '20 at 06:54
  • Note though that this is default behavior in almost all consoles. They typically already flush their output stream internally before any input to prevent exactly this from happening. Hence you will only encounter this problem if you are working with old consoles. – Zabuzard Sep 18 '20 at 07:00
  • @Hulk using cin.tie(0) unties std::cin and std::cout ,the output would not be flushed(ie does not appear on console) before the user's next input. In terms of competitive programming , let us say we have 2 test cases for a problem,the output for both the test cases would be flushed only after the program takes all the user inputs.Is there anything of this sort in java? – Siva Jagadesh Sep 18 '20 at 07:00
  • Javas streams are not tied to each other to begin with. It is the console that likely flushes them itself, not Java. You can lookup the source code at GitHub, there is no `flush` call. – Zabuzard Sep 18 '20 at 07:01
  • As a note, it would help your question if you would explain that method in detail. You only tagged `java` so dont expect to get people who know about `C++` (you might want to modify your tags). And, as you see, it already caused confusion. – Zabuzard Sep 18 '20 at 07:03
  • 1
    @Zabuzard Please review the question now.Also thanks for your help! – Siva Jagadesh Sep 18 '20 at 08:09
  • Im not an expert on this topic but I dont think that the streams are tied to each other in that way to begin with. I have seen mixed outputs on some consoles already. So I think this might just be the console messing with you. – Zabuzard Sep 18 '20 at 08:26
  • Your example output is extremely confusing as you didn't post the program that produces it. – Sebastian Redl Sep 18 '20 at 08:27
  • @SebastianRedl I have added the program , I hope it makes sense now. – Siva Jagadesh Sep 18 '20 at 08:36
  • Not needed in Java: https://stackoverflow.com/questions/7166328/when-why-to-call-system-out-flush-in-java – 9dan Sep 20 '20 at 03:05

1 Answers1

0

Some C libraries—notably including glibc—flush standard output when reading from certain input streams. You’ll have to use a platform-specific API call to change that behavior (possibly by making the input stream fully buffered); I don’t know of any standard/premade wrapper for such a function.

Davis Herring
  • 24,173
  • 3
  • 25
  • 55