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

Posted on 2006-06-01
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
    LVL 30

    Accepted Solution

    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.

    LVL 1

    Author Comment

    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.
    LVL 1

    Author Comment

    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.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
    C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (…
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now