Sarpedon
asked on
How to turn off characters echoed to the screen from keyboard input
I'm writing a console application that requires the user to enter a password and I would like the program to not echo the input from the keyboard while they enter the password or, if possible, to echo a placeholder like a *.
So I guess like to learn either how to turn off character echo, or how to redirect the standard output.
If it's any help, it's going to be a windows program compiled on MSVC++ 6.0
So I guess like to learn either how to turn off character echo, or how to redirect the standard output.
If it's any help, it's going to be a windows program compiled on MSVC++ 6.0
If you are writing a console app, use getpass() defined in conio.h.
If you are writing a GUI app, use a normal edit field and create it with the ES_PASSWORD style.
If you are writing a GUI app, use a normal edit field and create it with the ES_PASSWORD style.
>>If you are writing a console app, use getpass() defined in conio.h.
Not available on Win32.
Not available on Win32.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Sys_Prog.
JKR, You're code still output the text on the screen before outputting the *'s. Not sure if it was just my system.
Just one last thing, If I use getch() and output a * when they type a backspace is taken as a character and a * is output. Is there anyway for me to go backwards in the output?
JKR, You're code still output the text on the screen before outputting the *'s. Not sure if it was just my system.
Just one last thing, If I use getch() and output a * when they type a backspace is taken as a character and a * is output. Is there anyway for me to go backwards in the output?
Check for ascii value, if it is baskspace then do not output a *
Example
The following accepts char's in a while loop and stops on entering a escape
Does not print '*' for backspace
#include <stdio.h>
#include <stdlib.h>
int main ( void ) {
char ch ;
while ( ( ch = getch() ) != 27 ) {
if ( ch != 8 )
printf ( "*" ) ;
}
system ( "PAUSE" ) ;
}
BOOL CheckPwd(int nTries,char *pszPwd,char *pszPrompt)
{
int nInput=CR;
int nScrOff=4;
int nOffset;
int nPwdLen;
char *acInBuf;
BOOL bCorrPwd=FALSE;
nPwdLen=strlen(pszPwd)*siz
if (!(acInBuf=(char *)malloc(nPwdLen+2)))
{
printf("\n fatal: malloc() failed!\n\n");
abort();
}
memset(acInBuf,0,nPwdLen+2
for (int i=0 ; i<nTries; i++)
{
printf("\n\n %s",pszPrompt);
fflush(stdout);
nOffset=0;
nScrOff=4;
while(CR!=(nInput=fgetc(st
{
*(acInBuf+nOffset)=(char)n
if (!printf("%c",'*')
break;
fflush(stdout);
if (nOffset<=nPwdLen) nOffset++;
}
if (bCorrPwd=(!strcmp(pszPwd,
};
free(acInBuf);
return(bCorrPwd);
}
Simply call it like
if (!CheckPwd(3,"mypassword",
exit(-1);
which will allow 3 tries ;-)