Console User Input Done Right

This is something I have to deal with a lot when helping out on these forums. Instead of rewriting the same things over and over, I decided to just write an article that I could link to.

http://www.lb-stuff.com/user-input

Opinions welcome.

I would submit an actual article to this site but I wanted to have links in the code. Also, I have no idea if an existing article already says what I say in mine...
Last edited on
Too much water 7.8/10.
on serious note good stuff. Oh and typo.

You then have to deal with ignoreing everything the user has typed before doing anything further.
Last edited on
Oh i did notice that, guess I couldnt figure the significance.
i don't like the indentation of lines 10--13, second snip

you should use cerr to output error messages

> most consoles let you simply press the up arrow to retype the previous input
¿they do?

> Like "123 456abc 789"? Well, we can apply the same line-by-line thinking here too:
> break up the string into multiple strings, separated by spaces, and then process each one individually.
a- Read a line from cin as an string
b- Convert the string to an istringstream
c- Read a word from the istringstream as an string
d- Convert the string to an istringstream
e- Read a number

I don't think that you need steps c and d. After all, you can screw the stringstream as much as you want..
(step e would be a loop)

> then the input operation will fail without changing the variable you were inputting into
iirc, that was the previous standard. Now numbers are set to 0 in case of failure, and char arrays would be "empty".


you may want to explain why getline() is inside the loop


¿Not one mention to readline() ? http://linux.die.net/man/3/readline (or a c++ equivalent)
ne555 wrote:
you should use cerr to output error messages
Fixed.
ne555 wrote:
¿they do?
Is it an overzealous claim?
ne555 wrote:
a- Read a line from cin as an string
b- Convert the string to an istringstream
c- Read a word from the istringstream as an string
d- Convert the string to an istringstream
e- Read a number

I don't think that you need steps c and d. After all, you can screw the stringstream as much as you want..
(step e would be a loop)
Oops, you're right - fixed. Not sure what I was thinking with that.
ne555 wrote:
iirc, that was the previous standard. Now numbers are set to 0 in case of failure, and char arrays would be "empty".
I didn't know about that change, thanks for pointing it out - fixed via removal of false info.
ne555 wrote:
you may want to explain why getline() is inside the loop
Done.
ne555 wrote:
¿Not one mention to readline() ? http://linux.die.net/man/3/readline (or a c++ equivalent)
I considered discussing wrapping the boilerplate into functions, but my target audience is people who are new to C++. Do you think I should add a section for making functions out of this?

EDIT: I changed the indentation of the two code snippets - it's no longer copy-paste friendly.
Last edited on
Is it an overzealous claim?

yeah. its very environment dependent. for example running the program in bash (im too tired to remember what exactly causes this) on 14.04 ubuntu x64 recognizes up arrow as part of the input sequence. you would have to use a special lib for anything else (like libeditline)

¿Not one mention to readline() ? http://linux.die.net/man/3/readline (or a c++ equivalent)

thats not standard. is that POSIX? it doesnt look POSIX
@xkcd reference: I use bash on Windows, and up/down cycle previous inputs. I have also used various ssh programs o connect to my university's *nix machines and it is the same there too. Even the Windows console behaves this way, although not nearly as well.
Last edited on
yeah :L i figured it wasnt bash. that was a long shot. the point remains though that its environment dependent and not something that should be relied upon. i can take a pic of the behavior on my machine if youd like
Topic archived. No new replies allowed.