?
Solved

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

Posted on 2003-10-30
6
Medium Priority
?
283 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 10

Accepted Solution

by:
Sys_Prog earned 1000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 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.
Suggested Courses

765 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