Solved

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

Posted on 2003-10-30
6
281 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
Industry Leaders: 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!

 
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

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

Suggested Solutions

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

739 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