2

It is not uncommon to see a command-line program that prompts the user to enter a string to be processed in some way by the program. But is it possible to create a program that does this, and has a default value already there at the prompt - so that if the user wants to use the default value, they need only press Enter? (But if the user wants to use a different value, they will have to set about editing the pre-populated string - which might mean holding down backspace to delete it and then typing something else.)

Provisos:

  • Needs to work on Windows. Well, for my purposes it needs to work on Windows. You can suggest a solution that only works on other OSes if you like, but I won't accept your answer.
  • Needs to use only what is provided by the standard library.
  • It's not sufficient to just say, "leave input empty to use the default value" - that's merely a workaround.

I've seen versions of this question that are specific to other languages, but not one for C++.

Hammerite
  • 21,755
  • 6
  • 70
  • 91
  • 2
    Note: if the solution has to use only what is provided by the Standard Library, then it *should* be guaranteed work on any OS, not just Windows. So requirement 1 is somehow implied by requirement 2. – Andy Prowl Jan 20 '15 at 23:28
  • 3
    I don't think this is feasible using the Standard Library alone, but I can't prove it either. – Andy Prowl Jan 20 '15 at 23:34
  • This is essentially a duplicate of http://stackoverflow.com/q/1103933/33732, but with the stipulation that solutions be limited to the standard library. – Rob Kennedy Jan 20 '15 at 23:36
  • @RobKennedy then it's not a duplicate – Axalo Jan 20 '15 at 23:39
  • 1
    A common alternative which is way easier to implement is to have a default which will be used if the input was empty (and print the default value so the user can see it), like: `Select language [en]:` - it is implemented just with a print statement (and flush, since the line is unterminated), a getline, and then the logic "if empty then use default". – leemes Jan 20 '15 at 23:56
  • There's a solution demonstrated in Python at http://stackoverflow.com/a/5888246/33732. The equivalent C++ code would use functions from kernel32, which your program will certainly be using, even if you don't explicitly include *windows.h*. Does that break your standard-library requirement, even though you're only interested in Windows solutions anyway? – Rob Kennedy Jan 20 '15 at 23:58
  • Rob, I guess it would meet the actual need that motivated me to ask the question, though it wouldn't meet the standard of the question as I asked it. – Hammerite Jan 21 '15 at 12:55

1 Answers1

2

One can do it using just the standard library.
But one cannot do it with just the guarantees provided by the standard library.

What you need to have, for any solution, is a way to control the terminal (which implies a guarantee about being connected to a terminal, and knowledge of the idiosyncrasies in controlling said terminal.

Unfortunately, the windows console window is not a proper terminal, which can be controlled that way.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118