Is it possible to edit the cin input buffer?

Aug 2, 2012 at 6:03pm
Is it possible to start a cin that already has text in it? So like the input buffer already contains characters before the user types anything in.
Aug 2, 2012 at 6:42pm
Not really, the std::cin object is created in the iostream header, and AFAIK the constructor for istream objects doesn't take arguments. What are you trying to do? There is probably a better solution. Also streams really only save formatting data, they are there to interact with the hardware input.
Aug 2, 2012 at 6:57pm
See, I want to make a very simple editor in the console, so I want to have an editable string to begin with, if you could give me any pointers that would be great.
Aug 2, 2012 at 7:02pm
I'd just make a stream.
Aug 2, 2012 at 7:21pm
If you don't mind me asking, how would you do that?
Aug 2, 2012 at 7:59pm
Use a stringstream.
Aug 3, 2012 at 1:09pm
There is one other way!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class mystream
{
	stringstream stream;
	public:
	template<typename T>
	istream& operator>>(T& x)
	{
		return stream.str().empty()?cin>>x:stream>>x;
		}
	template<typename T>
	ostream& operator<<(const T& x)
	{
		return stream<<x;
		}
} myin;

Now, when you use myin<<x, it will put x into the input buffer! That way, myin>>x will read from cin iff the buffer ins empty, and from the buffer otherwise! I think that's just what you wanted! :D
Aug 3, 2012 at 2:02pm
bump
Aug 3, 2012 at 10:21pm
what are you bumping it for, and you sure do love templates
Aug 4, 2012 at 2:18am
Thanks viliml, workarounds are somewhat of an art
Topic archived. No new replies allowed.