Hello.
Im wondering does anyone know how to mask a users input in a C++ program. For example if someone was to type in a password you wouldnt want it not to appear in *'s.
Any ideas or help is appreciated, thank you.
- rootnix
Hello.
Im wondering does anyone know how to mask a users input in a C++ program. For example if someone was to type in a password you wouldnt want it not to appear in *'s.
Any ideas or help is appreciated, thank you.
- rootnix
you can use a combination of getch() to get a char from the keyboard and putchar('*').
getch() is a nonstandard function but almost every compiler has it although it may be called _getch() or even __getch().Itusually lives in conio.h .consult your help files.
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
Using _getch (from conio.h) you will take a character from stdin and not echo it. So you put a '*' out in its place after storing the character, example:
I'm sure there are more clever ways of do it but this should get you started.Code:#include "stdafx.h" #include <conio.h> #include <iostream> using namespace std; int main(int argc, char* argv[]) { char ch[30] = {0}; cout << "Please enter password: "; for ( int i = 0;30 > i;i++ ) //make sure we don't pass the end of the array { ch[ i ] = _getch( ); if( '\r' == ch[i] ) //break on carriage return break; cout << '*'; } cout << endl; //do what you want with password in the case I'll display it cout << "The password is: " << ch << endl; return 0; }
char pword[16];
.
.
.
cout<<"Password: ";
getch(pword);
putch('*');
i dont have my help files im running MS Vis c++ 6.0. i get an error that getch() doesnt take 1 parameter(s)....
thanks,
- rootnix
An online version of the help files (MSDN) is available at the MS site.i dont have my help files im running MS Vis c++ 6.0. i get an error that getch() doesnt take 1 parameter(s)....
Check Dang's example above on how to use getch().
zen