views:

127

answers:

6

Possible Duplicate:
Read a password from std::cin

I want to cin>>input but when they input something i don't want it to be visible to them. Like when you use passwd in unix to change your password where it doesn't show what you typed. i hope it is clear what i am asking. Thank you in advance.

+1  A: 

If what you want is something like what you do when you enter the password to login to a linux box, that behavior cannot be achieved in C or C++. You will have to use a platform specific library to do that. More specifically, console input in C and C++ is always echoed to the console.

Billy ONeal
+2  A: 

cin isn't the way to do this, since the OS (usually) echoes standard input. What you need to do is handle the key events at the OS-level.

TreDubZedd
+6  A: 

From C++ FAQ Lite

This is not a standard C++ feature — C++ doesn't even require your system to have a keyboard or a screen. That means every operating system and vendor does it somewhat differently. Please read the documentation that came with your compiler for details on your particular installation.
Leonid
RTFM is the best answer? Really?
indiv
Well, if we knew which platform is in question and what entity controls input/output then we could try to help out...
Leonid
@indiv - often times, yes.
Noah Roberts
+1  A: 

There is getpass(3) for Unix-like systems, which will do what you want.

However,

  1. It's not portable (Unix systems only).
  2. It's deprecated ("Present in SUSv2, but marked LEGACY. Removed in POSIX.1-2001.").

There is no way to do it with standard C or C++.

Alex B
A: 

Have it detect key presses. Think of it this way: When a user presses the 'A' key in a video game, the program runs code; don't think of it as receiving input. Think of it as detecting keystrokes and adding each keystroke to some data structure (e.g. an array).

I am familiar with the functionality in UNIX terminal that you are talking about and it doesn't allow you to backspace or anything. If you want that functionality, just do what I said above.

If you want to be able to backspace, although the user won't be able to see what they backspace, you can write code that removes the last element of the array when the user clicks backspace.

Also, everyone else is correct when they say that you will need to program this specific to the system you're running it on.

Lastly, this might not be secure if you are using this for a password and just throwing it into an array.

OOProg
A: 

Use something like pdcurses.

rlbond