Solved

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

Posted on 2003-10-30
6
278 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

809 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