Click to See Complete Forum and Search --> : Input check


arlenagha
04-11-2003, 02:25 AM
is there a function in iostream library that checks for user input(keys) while the user is typing.

i am writting a code that asks the user to enter a binary number:

cout << "Enter binary number-> ";

i want to check the key enteries while the user is typing so that i can give error if the user enter anything but 1s and 0s.

BTW this is a console app.

binaryDigit
04-11-2003, 01:59 PM
you'd have to use ncurses or the termios library to do that.
here's an example of using the termios library to do that. it's in C but it should get you on the right track.
http://www.codeexamples.org/cgi-bin/c2h/hl.cgi?filename=userinput.c&type=HTML-detail

Stuka
04-11-2003, 02:25 PM
You could always create a custom class using the appropriate members of an istream class that only allowed 1's and 0's as input. ;)

bwkaz
04-11-2003, 05:22 PM
You could always pull the whole string in, then check it afterward. That would be the most cross-platform way to do it, anyway.

Stuka
04-11-2003, 05:32 PM
Yeah, but creating your own iostream-compliant binary number input class would be soooo l337!
;)

bwkaz
04-11-2003, 07:36 PM
I ... err... suppose... :D

arlenagha
04-12-2003, 12:56 AM
it doesn't have to be binary input checker. i just wanted to be able to check the user input in the console as the keys where pressed. i guess that would be to check the buffer ?!?

Stuka
04-14-2003, 09:33 AM
Sorry about that...I can go off on really bizarre tangents at times. But essentially, a custom istream operator WOULD check the buffer and drop invalid input. However, you have to be pretty familiar with the iostreams in general in order to do that. I for one am NOT that good with them, though I do know that such a thing would be possible. A simpler, less sexy way would be just to read the data into a string, and use the remove_if algorithm to pull out any non-binary input.