If you do not like the cheap version, here is a solution without conio.h for Windows
#include <windows.h>
#include <strsafe.h>
#include <iostream>
int main()
{
char letter;
DWORD cNumRead;
INPUT_RECORD irInBuf[128];
std::cout << "enter 'q' to quit\n";
do {
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
if (hStdin == INVALID_HANDLE_VALUE) exit(1);
if (!SetConsoleMode(hStdin, ENABLE_WINDOW_INPUT))
exit(2);
if (!ReadConsoleInput(hStdin, irInBuf, 128, &cNumRead)) {
DWORD dw = GetLastError();
std::cout << "System Error: " << dw << "\n"
exit(3);
}
for (unsigned i = 0; i < cNumRead; i++) {
switch (irInBuf[i].EventType)
{
case KEY_EVENT: if (irInBuf[i].Event.KeyEvent.bKeyDown) {
letter = irInBuf[i].Event.KeyEvent.uChar.AsciiChar;
std::cout << letter;
}
break;
default:
letter = ' ';
}
}
} while (letter != 'q');
}