C++, getche(), cin.get()

I am wondering why there is no standard function in C++ that would operate like getche() or getch() that is part of conio.h.

The get() function in istream requires that the enter key be pushed to get the user input.

Would there be anyway to send a carriage return to the keyboard buffer so it mimics a the enter key being pressed?

Thank You, Darnell
The get() function in istream requires that the enter key be pushed to get the user input.

It doesn't, actually. It's the terminal that's waiting.

The terminal emulator doesn't put your input into your program's buffer until you press enter, so that you can edit what you typed before the program sees it. You can convince yourself of this by redirecting input from a file. Even if the file contents are on exactly one line, all the data will be read without delay.

I am wondering why there is no standard function in C++ that would operate like getche() or getch() that is part of conio.h.
getch() has to talk to the terminal, but the C++ streams interface doesn't care about the particular terminal the user is operating, and can't really mandate its behavior. In fact, under Unix-like systems, getch() is part of libcurses, a non-standard library which does exactly this: it talks to the terminal and does terminal-specific things.

Further, using getch() is usually a bad idea. Console programs should only rarely be interactive. The core strength of the command line is versatility: well written command line tools can be automated easily because they don't print lots of irrelevant output and they don't require the user to sit there to walk through all the prompts.

If you want an interactive program, just write a proper GUI.
Last edited on
Thank You, Its been a mystery to me.
Topic archived. No new replies allowed.