Created
March 11, 2019 18:34
-
-
Save tbhaxor/fb973f201077a58c9296b9dfd8f9cc6f to your computer and use it in GitHub Desktop.
Get user input in ncurses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdlib> | |
#include <cstring> | |
#include <ncurses.h> | |
#include <iostream> | |
using namespace std; | |
int main() | |
{ | |
// setting up string | |
initscr(); | |
// setting up true Password | |
string pass = "pass@123"; | |
// allocating space for input Password | |
char *input = (char *)malloc(10); | |
// printing | |
// LINES means number of rows | |
// COLS means number of columns | |
mvprintw(LINES / 2, COLS / 2 - 14, "Enter Password "); | |
noecho(); // suppress character echoing | |
wgetnstr(stdscr, input, sizeof(input)); // used to get user input from specific window (here, stdscr) | |
// clearing screen | |
clear(); | |
refresh(); | |
// comparing Password | |
if (strcmp(pass.c_str(), input) == 0) | |
{ | |
// Password is correct | |
attron(A_BOLD); | |
mvprintw(LINES / 2, COLS / 2 - 6, "Welcome User"); | |
attroff(A_BOLD); | |
} | |
else | |
{ | |
// Password is wrong | |
attron(A_BLINK); | |
mvprintw(LINES / 2, COLS / 2 - 9, "Unauthorized Access"); | |
attroff(A_BLINK); | |
} | |
// pausing window | |
getch(); | |
// ending screen | |
endwin(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment