Input without the enter ('\n')

Whenever your input something using cin >> thing, the user always has to press enter for the the program to evaluate anything as input,
How would I input something by just pressing the character?
(Instead of "c\n" it’s "c")
Last edited on
It's possible, but all answers depend on you telling us which OS/Compiler you're using.

There is no standard C++ feature you can use.

So you have to resort to something specific.
In windows it can be as easy as this:

1
2
3
4
5
6
#include <conio.h>

int main() {
    for (int c; (c = _getch()) != EOF; )
        std::cout << "=== " << char(c) << " ===\n";
}

In *nix you can simulate the conio functions.

For portable code, you should use a library like ncurses.
https://www.google.com/search?q=ncurses
Um oops. Sry I forgot to check after so many times, thanks okay yeah I think I’ll look into ncurses more.
Topic archived. No new replies allowed.