cin and getch()

Discussion in 'C++' started by Crow, Mar 13, 2006.

  1. Crow

    Crow Guest

    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.
     
    Crow, Mar 13, 2006
    #1
    1. Advertising

  2. Crow

    Jim Langston Guest

    "Crow" <> wrote in message
    news:...
    > 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.
     
    Jim Langston, Mar 13, 2006
    #2
    1. Advertising

  3. 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.
    --
    <mailto:> <http://www.dietmar-kuehl.de/>
    <http://www.eai-systems.com> - Efficient Artificial Intelligence
     
    Dietmar Kuehl, Mar 13, 2006
    #3
  4. 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
     
    n2xssvv g02gfr12930, Mar 13, 2006
    #4
  5. 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.
    --
    <mailto:> <http://www.dietmar-kuehl.de/>
    <http://www.eai-systems.com> - Efficient Artificial Intelligence
     
    Dietmar Kuehl, Mar 13, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TaiwanNoWhere

    cin and cin.clear() problem

    TaiwanNoWhere, Oct 13, 2003, in forum: C++
    Replies:
    8
    Views:
    4,109
    P.J. Plauger
    Oct 17, 2003
  2. Chris Mantoulidis

    std::cin.ignore() and std::cin.clear()

    Chris Mantoulidis, Jan 6, 2004, in forum: C++
    Replies:
    5
    Views:
    17,183
    Kevin Saff
    Jan 6, 2004
  3. voidstar
    Replies:
    3
    Views:
    10,104
    Mark R Rivet
    Jul 27, 2004
  4. Aleander

    cin and cin.getline()

    Aleander, Mar 6, 2005, in forum: C++
    Replies:
    5
    Views:
    8,715
    Alex Vinokur
    Mar 6, 2005
  5. Fernando
    Replies:
    4
    Views:
    1,697
    Fernando
    Nov 16, 2011
Loading...

Share This Page