Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > cin and getch()

Reply
Thread Tools

cin and getch()

 
 
Crow
Guest
Posts: n/a
 
      03-13-2006
Is there any way to make cin behave like getch()? Specifically,
getch() returns immediately after a key is pressed and the cin family
of input methods seem to block until a new line is encountered. I need
some help.

 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      03-13-2006
"Crow" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> Is there any way to make cin behave like getch()? Specifically,
> getch() returns immediately after a key is pressed and the cin family
> of input methods seem to block until a new line is encountered. I need
> some help.


No.


 
Reply With Quote
 
 
 
 
Dietmar Kuehl
Guest
Posts: n/a
 
      03-13-2006
Crow wrote:
> Is there any way to make cin behave like getch()?


Based on the standard, the answer is "no". The reason for this is
that the behavior of 'getch()' is not present on all systems where
'std::cin' still makes sense and there is actually nothing which
requires that 'std::cin' is connected to a keyboard at all. In fact,
it often isn't.

That said, on typical desktop systems there are calls which can be
used to suppress the usual line buffering mode. On a POSIX system
you can use 'tcgetattr()' and 'tcsetattr()' for this purpose. The
details of this are, however, not covered by the C++ standard and
you thus need to ask in an environment specific forum for a
solution.
--
<(E-Mail Removed)> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
 
Reply With Quote
 
n2xssvv g02gfr12930
Guest
Posts: n/a
 
      03-13-2006
Crow wrote:
> Is there any way to make cin behave like getch()? Specifically,
> getch() returns immediately after a key is pressed and the cin family
> of input methods seem to block until a new line is encountered. I need
> some help.
>


Look up the member function get() for std::istream derived classes. This
should do what you require in most cases. Alternatively you could create
your own version of getch() using std::cin, although I'd recommend you
learn to use the STL stream classes.

char ch;
std::cin.get(&ch);

JB
 
Reply With Quote
 
Dietmar Kuehl
Guest
Posts: n/a
 
      03-13-2006
n2xssvv g02gfr12930 wrote:
> Crow wrote:
>> Is there any way to make cin behave like getch()? Specifically,
>> getch() returns immediately after a key is pressed and the cin family
>> of input methods seem to block until a new line is encountered. I need
>> some help.
>>

>
> Look up the member function get() for std::istream derived classes. This
> should do what you require in most cases.


Actually, it doesn't! 'get()' is mostly likely to block until input
becomes available and for input from the keyboard this normally means
until the user hits the return key.

If you want to use standard streams and still get the same behavior
like using 'getch()' you need to fallback to platform specific
approaches like using 'getch()' inside a stream buffer or setting up
standard input to not use line buffering mode.
--
<(E-Mail Removed)> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using cin.get() and cin.eof() ends up in an endless loop Fernando C++ 4 11-16-2011 07:19 PM
cin and cin.getline() Aleander C++ 5 03-06-2005 03:57 PM
std::cin.ignore() and std::cin.clear() Chris Mantoulidis C++ 5 01-06-2004 11:08 PM
cin and cin.clear() problem TaiwanNoWhere C++ 8 10-17-2003 05:53 PM
cin and spaces Samuele Armondi C++ 2 06-23-2003 10:28 PM



Advertisments