The Age Old Getch Problem: Need A C++ Replacement

Posted on 2006-06-01
Medium Priority
Last Modified: 2011-10-03
After several years of programming, successfully I might add, on the windows platform using borland's c++ compiler, it has become painfully evident that after all these years, it has not yet become apart of the standard library to include to include an equivalent for the <conio> header file, even if it means using platfom specific code on the different compilers. I don't see the problem.

But anyway, my problem is that regardless of the reasons many persons, have proposed for using cin.get() or getchar() they still dont have the the functionallity of getch() to return each keypress character code, with echo turned off. Now I have recently learned that there is a way in linux/unix to cause getchar() to behave similarly to getch() by using functions from the <termino> header file (or something like that). So my question is, isn't there someway of telling cin.get() or getchar() to return that int code or char for the key pressed instead of waiting for newline to be input.

If someone knows a custom function that can deliver that particular functionallity, that would be good, but i would really like to see if there is a standard way it can be done.
Question by:mkandre
  • 2
LVL 30

Accepted Solution

Axter earned 600 total points
ID: 16805988
Hi mkandre,
> So my question is, isn't there someway of telling cin.get() or getchar()
> to return that int code or char for the key pressed instead of waiting
> for newline to be input.

On windows&DOS, most compilers support a getch().
On UNIX/Linux, you should be able to implement the termino method.
So it wouldn't be that hard to create a wrapper function for the UNIX/Linux platform, and just add a #ifdef around the wrapper function.
That would make your code portable on Windows, UNIX, and Linux.


Author Comment

ID: 16813495
well unfortunately GCC g++ compiler for windows doesn't seem to support <conio> or even <conio.h>. Well I'm hoping someone does know some to do it, or how to simulate the getch() functionality.

Author Comment

ID: 16843162
Ok, fine. I guess this is a lost cause.

Since you did give a comment that had somewhat usefull information, I guess it's only fair I award you the points Axter.


Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

755 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question