Solved

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

Posted on 2003-10-30
6
276 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

  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 …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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++.

707 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

13 Experts available now in Live!

Get 1:1 Help Now