How to disable/enable displaying user input in console

K

kimso.zhao

Hi All,

I need a function that when user types a password in the console, the
password should be invisible (e.g. display nothing or display "*"
instead). Just as you can see when you login a machine.

How to implement this? Thanks!
Note: user is typing passord in the console.
 
R

Rolf Magnus

Hi All,

I need a function that when user types a password in the console, the
password should be invisible (e.g. display nothing or display "*"
instead). Just as you can see when you login a machine.

How to implement this? Thanks!
Note: user is typing passord in the console.

There is no standard C++ way to do that, because the C++ doesn't define a
console, but rather only an input stream (that may be connected to a
keyboard or not) and some output streams (that may be connected to a
display or not). You will need some platform-specific functions.
Maybe the (n)curses library can help you.
 
R

Ron Natalie

Hi All,

I need a function that when user types a password in the console, the
password should be invisible (e.g. display nothing or display "*"
instead). Just as you can see when you login a machine.

How to implement this? Thanks!
Note: user is typing passord in the console.
What consoles do is system dependent. You need to ask
in a group for your operating system (and possibly
windowing system) ... windows, unix, etc...

C++ doesn't address this.
 
J

Jim Langston

Hi All,

I need a function that when user types a password in the console, the
password should be invisible (e.g. display nothing or display "*"
instead). Just as you can see when you login a machine.

How to implement this? Thanks!
Note: user is typing passord in the console.

Use whatever OS specific call you have to read a key from the keyboard. It
is different for each OS. Then get a keypress, and print "*" to the screen.

char Input;
std::string Password;
while ( Input = FunctionToGetKeypress() != 13 ) // 13 is usually enter
{
Password += Input;
std::cout << "*";
cout.flush(); // May or may not be needed
}
std::cout << std::endl;

for Windows it's getch()
Not sure for linux.
check for your OS what function it is to get a single key press.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top