Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we need to tie std::cin and std::cout?

Tags:

By default, the standard input device is tied together with the standard output device in the form: std::cin.tie (&std::cout); which guarantees that the output buffer has been flushed before input is invoked. So I try to untie them by using std::cin.tie(0), but it seems that the result, has no difference with the tied one.

#include<iostream> using namespace std;  int main(int argc, char *argv[]) {     char c;      cin.tie(0)      cout << "Please enter c:";     cin >> c;     cout << c ;      return 0; } 

Am I testing wrong? Why do we need to tie them together? Do they share the same buffer?

like image 413
std Avatar asked Dec 27 '12 09:12

std


People also ask

Why do we use cin and cout statements?

cin is an object of the input stream and is used to take input from input streams like files, console, etc. cout is an object of the output stream that is used to show output. Basically, cin is an input statement while cout is an output statement.

Why CIN tie is used in C++?

By default cin is tied to cout to ensure a sensible user interaction. For example: std::cout << "Enter name:"; std::cin >> name; If cin and cout are tied, you can expect the output to be flushed (i.e., visible on the console) before the program prompts input from the user.

Why do I need std :: cout?

Difference between cout and std::cout in C++ The cout is a predefined object of ostream class, and it is used to print the data on the standard output device. Generally, when we write a program in Linux operating system for G++ compiler, it needs “std” namespace in the program.

Why do we use STD before cout in C++?

You probably had using namespace std; before in your code you did in class. That explicitly tells the precompiler to look for the symbols in std , which means you don't need to std:: . Though it is good practice to std::cout instead of cout so you explicitly invoke std::cout every time.


2 Answers

There is nothing wrong in your example (except that you should add a semi-colon after the cin.tie(0) line), nor with the way iostream objects work.

tie() simply guarantees the flushing of cout before cin executes an input. This is useful for the user to see the question before being asked for the answer.

However, if you un-tie() the cin from cout, there is no guarantee that the buffer of the cout is flushed. But there is no guarantee that the buffer is un-flushed neither. In fact, if the computer has enough resources, it will flush the cout buffer immediately, so this occurs before cin asking for the input. This is the case in your example.

So, everything works well. Except that after cin.tie(0), there is no guarantee that the flush-ing will occur. However, in 99% of the cases, that flush-ing will still occur (but it is no longer guaranteed).

In theory, if tied, cin and cout could share the same buffer. But, I think no implementation does that. One reason is that the two may be un-tie()d.

like image 72
user1284631 Avatar answered Oct 11 '22 09:10

user1284631


I think, previous answer is wrong (and I wonder why is it so upvoted and marked as true, being obviously not).

To break happens-before tie, you shall (in the case of standard io only) (1) remove sync with stdio and (2) untie streams.

Like this:

std::cin.tie (nullptr); std::cout.sync_with_stdio(false); std::cout << "Please enter c: "; std::cin >> c; 

Then you are guaranteed to have untied streams. Syncing with stdio is special ability in order to have proper happens-after ordering for C-style and C++-style input and output and I strongly discourage you from removing it without real necessity.

like image 20
Konstantin Vladimirov Avatar answered Oct 11 '22 08:10

Konstantin Vladimirov