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

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

Posted on 2003-10-30
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
Question by:Sarpedon
LVL 86

Expert Comment

ID: 9651477

BOOL CheckPwd(int nTries,char *pszPwd,char *pszPrompt)
int  nInput=CR;
int  nScrOff=4;
int  nOffset;
int  nPwdLen;
char *acInBuf;


if (!(acInBuf=(char *)malloc(nPwdLen+2)))
     printf("\n fatal: malloc() failed!\n\n");


for (int i=0 ; i<nTries; i++)

    printf("\n\n %s",pszPrompt);


             if (!printf("%c",'*')

             if (nOffset<=nPwdLen) nOffset++;


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



Simply call it like

if (!CheckPwd(3,"mypassword","Password: "))

which will allow 3 tries ;-)
LVL 23

Expert Comment

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.

LVL 86

Expert Comment

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

Not available on Win32.
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

LVL 10

Accepted Solution

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

Author Comment

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?
LVL 10

Expert Comment

ID: 9655356

Check for ascii value, if it is baskspace then do not output a *


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

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Writing a parser for java language 4 83
C++ Language error 28 245
SNMP error No Such Object available on this agent at this OID 3 298
c++ syntax question 9 52
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

808 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