Tricky question about i/o streams

Okay, so Im not quite sure how to explain this, so please bear with me. Im running MSVC++ 2010 on Windows 7, working with command line programs.

When cin is used to get input, it takes the input typed by the user after pressing enter and assigns it to the recipient in question. (not rocket science, I know)

What I didnt notice for a while was that if you entered a few commands, then pressed the up or down keys while the program was waiting for input, previous inputs were "autofilled" into the waiting input, but not sent unless the user then pressed enter. I am confident that the buffer is getting flushed regularly, so the stored inputs shouldnt be hiding in there, but otherwise I have no clue where they are coming from. I vaguely suspect the iostream input stream of doing it somehow, but it could be a windows specific thing as well.

So basically my two questions are:
-What is it?
-Is it possible to insert values into wherever these suggestions are coming from without the user inputting them manually? If I can do that I might be able to make navigating the command line a fair bit easier by making the task one of scrolling up or down, instead of manually typing identifiers.

Thanks for any help, and my apologies for the oddly worded question. Its a wee bit hard to describe.
closed account (Dy7SLyTq)
dont worry, its not odd. are you using an ide? because this isnt natural in c++. it has no knowledge of what arrow keys are. normally to do something like that you would have to use ncurses and write a buffer of your own to go back through.
I'm using Microsoft Visual Studio C++ 2010. I take it you think that the IDE is layering some functionality over the basic C++ code when it compiles?
closed account (Dy7SLyTq)
yeah, because its not something you can do by accident. if i could be so bold, download code::blocks w/ mingw and see if the "problem" exists. its always better to compile software on as many compilers as possible. if i were to write something using cin and used an up key it would just print something likek [[some_escape_char_here
It's not your program that handles all that. The thing that you see outputs on and that lets you enter input is a program too. Normally input is line buffered, meaning it will not give the input to your program until you press enter. It could send input right away but that would not allow you to correct mistakes by using backspace.

Your program simply receives and outputs data as a stream of characters. It's possible to redirect both input and output from and to files. Your program works the same no matter where the input comes from or where output ends up.
It's not the program that's doing that, nor is it the IDE that's "adding" this functionality to the program.

Try this:
1) Press Windows key + R (to open the Run dialog), type "cmd" and hit Enter.
2) Type some random gibberish (doesn't matter what, as long as it's not something like "format C:" or "shutdown -s -t 0" or anything like that) and hit Enter a few times while doing that (you should end up with a bunch of messages about your random gibberish not being recognized as a command or program).
3) Now, experiment with the up and down arrow keys and see what happens. :)

In short, it's the console interface that's providing this "previous input history" buffer.
In fact, if you right-click the title bar and click "Properties" (hopefully there's a button there -- or at least it's there in Windows XP), you'll see some options for "Command History" which controls how many of those previous inputs you see.

Basically speaking, it's an added bonus for Windows users (though it's not something you can control within your program -- or at least there's no way to access the buffer, though there are a couple of WinAPI functions that lets you change the settings for it).
Topic archived. No new replies allowed.