"It would be very rare that you would want cout not to be flushed when calling cin" I think you didn't mean to put that "not"..
Can you give an example how how cin.tie(NULL) can be harmful? One application for this would be a quiz program that uses getch() [it's an mcq quiz and we're using getch() because it's spontaneous and takes one input which is exactly what we want], here, this can be a problem if your questions have delays. As if the person clicks a key while the cout statement is under execution, the getch() would have already recognized to take that key that was pressed as input. So the user could mistakely give a wrong answer even thought the question was not even finished displaying!
What I did was use fflush(stdin). I didn't try cin.ignore().
So cin.tie(NULL) would be quite handy.. right? What are demerits for using this? For example does it take a lot of time?
From my understanding, writing this line before cin statements would make it so that the input buffer is flushed when input is encountered.
Also how to toggle this tie()? If it's possible!
"Input from the console is what cin does since "cin" stands for Console INput and "cout" stands for Console OUTput. "
The context was that cin could read from the input buffer even when the cin statement has not been reached.
For example:
1 2 3 4 5 6 7 8
|
#include<windows.h> // for sleep
#include<iostream.h>
int num;
cout<<"Type something even though cin has not been encountered yet";
Sleep(10*1000) // I think sleep takes milliseconds as parameter
cout<<"Type a number"
cin>>num;
cout<<num;
|
You will notice that when you type after "Type something even though cin has not been encoutered yet" whatever you had typed would go to the cin for num. Similarly with getch.
That's what I originally meant.