Skip to content

Instantly share code, notes, and snippets.

@tbhaxor
Created March 11, 2019 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tbhaxor/fb973f201077a58c9296b9dfd8f9cc6f to your computer and use it in GitHub Desktop.
Save tbhaxor/fb973f201077a58c9296b9dfd8f9cc6f to your computer and use it in GitHub Desktop.
Get user input in ncurses
#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