Thread: getch() equivalent in C++

  1. #1
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163

    getch() equivalent in C++

    I'm not quite good with C++'s standard library, I need help. Is there a function which will behave like getch() (yes, I know it's not C's standard either), that is, return after exactly one character typed into the standard input stream? I tried cin.get(char&) and int cin.get() but it didn't work. I searched, too, but it proved useless.
    Vanity of vanities, saith the Preacher, vanity of vanities; all is vanity.
    What profit hath a man of all his labour which he taketh under the sun?
    All the rivers run into the sea; yet the sea is not full; unto the place from whence the rivers come, thither they return again.
    For in much wisdom is much grief: and he that increaseth knowledge increaseth sorrow.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There is no standard way defined. You have to use non-standard extensions, which would depend on what OS/Compiler/Run-Time Library you are using how you achieve that. I expect that there isn't a way to (trivially) do that with cin - you'd have to use system specific code (such as include <conio.h> and use getch() if that exists in your implementation).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    As matsp said, there is nothing in the standard library to help you, but you can call getch() in C++ just as easily as you can in C.

  4. #4
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163
    So I thought.. Thanks!
    Vanity of vanities, saith the Preacher, vanity of vanities; all is vanity.
    What profit hath a man of all his labour which he taketh under the sun?
    All the rivers run into the sea; yet the sea is not full; unto the place from whence the rivers come, thither they return again.
    For in much wisdom is much grief: and he that increaseth knowledge increaseth sorrow.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. equivalent to getch in c++
    By Elkvis in forum C++ Programming
    Replies: 1
    Last Post: 02-05-2009, 01:07 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. kbhit() and getch() Equivalent..
    By ThLstN in forum C Programming
    Replies: 1
    Last Post: 03-11-2008, 02:44 AM
  4. Pls repair my basketball program
    By death_messiah12 in forum C++ Programming
    Replies: 10
    Last Post: 12-11-2006, 05:15 AM
  5. Replies: 0
    Last Post: 11-01-2002, 11:56 AM