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?