Default input value

Is it possible to create a default input value that will display the default value in a console window?

For example, have an input value of:

Input:

And the default value will be:

Input: Default Value

The default value will be displayed and the user can press backspace to clear if they want to change, otherwise just press enter.
closed account (EzwRko23)
Usually this is done differently:
 
Do you really want to delete this file [no]?: _


Now, if the user hits enter, instead of writing "yes", the application reads an empty string and can take "no" as a default.


Last edited on
If you want to do that sort of things, you should use a library like pd/ncurses
You can do it with standard streams but the result won't be really nice:
1
2
3
4
5
6
string value;
string default_value = "Hello";
cout << "Input : " << default_value << string(default_value.size(),'\b');
getline(cin,value);
if ( value == "" )
    value = default_value;


output the default value and then print some backspace characters to move back
get input
if the user just pressed enter the input value will be an empty string
so set it to the default value


Notice that if the user enter "hi", it will see "hillo" but the value will be only what was entered ( "hi" )
Thanks, that is what I was looking for, I'd like to keep within the standard libraries if possible. If not, I'll try the first method of displaying the value before the input box.

Using the last method, is it possible to somehow get the cursor behind the default Hello?
The backspaces should move the cursor back
( Unless you print a newline )
Last edited on
Great, thanks!
Topic archived. No new replies allowed.