Using termios.h for input masking instead of ncurses
We decided that using ncurses wasn't the best idea, so I wrote up this solution and included a conio.h solution for Windows.
#ifdef _WIN32 #include #else #include #endif std::cout << "Enter PIN: "; #ifdef _WIN32 for (unsigned int i = 0; i < 4; ++i) { pin[i] = _getch(); std::cout << "*"; } #else static struct termios old_t, new_t; tcgetattr(STDIN_FILENO, &old_t); new_t = old_t; new_t.c_lflag &= ~(ECHO) & ~(ICANON); tcsetattr(STDIN_FILENO, TCSANOW, &new_t); for (unsigned int i = 0; i < 4; ++i) { pin[i] = getchar(); putchar('*'); } tcsetattr(STDIN_FILENO, TCSANOW, &old_t); #endif pin[4] = '\0'; std::cout << std::endl;
Basically for termios you grab a struct describing the terminal settings and set the flag to not include ECHO. Β ICANON is the flag describing if everything is in "canonical mode." In my case, since the flag is set by default, it was causing the inputs to be stored in a buffer until a newline/return character was detected.
More painful than the ncurses version, but hey, you learn something new every day. Β One of those things I learned was that ncurses does not work in mingw/cygwin/on windows at all (cygwin you need a commercial license to use ncurses).
















