Solved

How to turn off characters echoed to the screen from keyboard input

Posted on 2003-10-30
6
277 Views
Last Modified: 2010-04-02
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
0
Comment
Question by:Sarpedon
6 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 9651477
Try

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)*sizeof(char);

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(stdin)))
         {  
            *(acInBuf+nOffset)=(char)nInput;

             if (!printf("%c",'*')
                 break;
             fflush(stdout);

             if (nOffset<=nPwdLen) nOffset++;

         }

       if (bCorrPwd=(!strcmp(pszPwd,acInBuf))) break;

 };

free(acInBuf);
return(bCorrPwd);
}


Simply call it like

if (!CheckPwd(3,"mypassword","Password: "))
  exit(-1);

which will allow 3 tries ;-)
0
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 9651533
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.

0
 
LVL 86

Expert Comment

by:jkr
ID: 9651587
>>If you are writing a console app, use getpass() defined in conio.h.

Not available on Win32.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 10

Accepted Solution

by:
Sys_Prog earned 250 total points
ID: 9652159
I think he can simply use getch() so that the entered characters do not get displayed.
0
 

Author Comment

by:Sarpedon
ID: 9652633
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?
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9655356

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" ) ;
}
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now