Computer Association of SIUE - Forums

CAOS Forums => Questions and Answers => Topic started by: JR on 2006-03-20T13:18:26-06:00 (Monday)

Title: password masking/hiding in c++
Post by: JR on 2006-03-20T13:18:26-06:00 (Monday)


who knows how to show asterisks instead of the user's input in c++ ??

someone said something a/b masking or turning off echo ???

JR
Title: Re: password masking/hiding in c++
Post by: Peter Motyka on 2006-03-20T13:43:55-06:00 (Monday)

const int passwdLength = 16;
char passwd [passwdLength];

for(int i=0; i <= passwdLength; i++)
{
    passwd[i] = getch();
    if (passwd[i] == 13) break;
    putch('*');
}


Needs some error handling/checking, but you get the idea...
Title: Re: password masking/hiding in c++
Post by: JR on 2006-03-20T22:14:03-06:00 (Monday)

well, if 'getch()' == cin.get()  then thats not what im lookin for b/c the user still has to type (and the console is displaying what they are typing) and then hit enter before anything happens.

really, im looking for a trigger that something has been typed (one char). if i can get to that point, i can clear it and display *.


OR even easier, somehow turn off the user display for that statement....is it called echo ??

JR





Title: Re: password masking/hiding in c++
Post by: Jerry on 2006-03-20T22:51:34-06:00 (Monday)
In C programs we used to use curses.h which defined an echo() and noecho(). These controlled whether a character that was typed in was displayed by getch().

Title: Re: password masking/hiding in c++
Post by: JR on 2006-03-20T22:53:52-06:00 (Monday)

aaahhhh. that would explain me having no idea what getch() was. thanks dr. w

JR
Title: Re: password masking/hiding in c++
Post by: Peter Motyka on 2006-03-20T22:59:10-06:00 (Monday)
Did you try compliling/running the code I posted?  It does exactly what you are describing as I tested it before posting.  BTW, my environment was Visual Studio 2003 on Windows XP.  I'd be curious if you had different results on a same/different platform.

Title: Re: password masking/hiding in c++
Post by: Geoff Schreiber on 2006-03-20T23:06:44-06:00 (Monday)
You have to include the conio.h library in vc++ to get getch and putch...  Both of these commands disable echo by bypassing the input buffer.  getchar and putchar enable echo.


#include
int main()
{
const int passwdLength = 16;
char passwd [passwdLength];

for(int i=0; i <= passwdLength; i++)
{

passwd[i] = getch();
if (passwd[i] == 13) break;
putch('*');
}


return 0;
}
Title: Re: password masking/hiding in c++
Post by: JR on 2006-03-21T01:56:02-06:00 (Tuesday)
sweet. conio.h did it. very cool. thanks guys


JR
Title: Re: password masking/hiding in c++
Post by: Kaitlyn Schmidt on 2006-03-23T15:19:20-06:00 (Thursday)
This IS very cool!!! Thanks for the code! I'm going to go play with it some more. :-D