Reputation: 3414
Any idea how to get like this feature in c or c++
password= ********
but at the end system gets right password(means real input) but other people see this.
Upvotes: 2
Views: 806
Reputation: 1264
For implementing without using library, you can use getch to get pressed character and print * for any character got from getch and use any key for for escape (here I use Enter).
Sample code
while(true) {
ch = getch();
if(ch== 13) // ASCII Code for Enter Key
break;
cout<<"*";
}
Upvotes: 1
Reputation: 2984
turn off echo
http://www.cplusplus.com/forum/general/12256/
windows
#include <windows.h>
void echo( bool on = true )
{
DWORD mode;
HANDLE hConIn = GetStdHandle( STD_INPUT_HANDLE );
GetConsoleMode( hConIn, &mode );
mode = on
? (mode | ENABLE_ECHO_INPUT )
: (mode & ~(ENABLE_ECHO_INPUT));
SetConsoleMode( hConIn, mode );
}
posix
#include <termios.h>
#include <unistd.h>
void echo( bool on = true )
{
struct termios settings;
tcgetattr( STDIN_FILENO, &settings );
settings.c_lflag = on
? (settings.c_lflag | ECHO )
: (settings.c_lflag & ~(ECHO));
tcsetattr( STDIN_FILENO, TCSANOW, &settings );
}
Upvotes: 0
Reputation: 288140
To confirm the password of a user, use pam (the official page is hosted on kernel.org and therefore down as of writing), in particular pam_authenticate
.
If your application has its own authentication architecture (and most of the time, it shouldn't), execute stty -echo
, ask for the password, and then execute stty echo
to restore the original behavior.
Upvotes: 3
Reputation: 41676
You need to tell the terminal that it suppresses the echo during the input. Maybe your operating system already provides a function that reads a password from the terminal. It might be called getpassword
or getpass
or getpasswd
.
See the command stty
to get an overview of the terminal options.
Upvotes: 2