Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

passwords and cin

Posted on 1999-07-14
13
Medium Priority
?
306 Views
Last Modified: 2010-04-16
Hello,
  I want to write a console program that allows me to set usernames and passwords. But when the users are entering new passwords, I don't want the cin function to show whatever I've typed for my password. How do I do that? Is it possible to do this with cin?
0
Comment
Question by:jernhung
[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
  • 4
  • 3
  • 3
  • +3
13 Comments
 

Expert Comment

by:bbarnette
ID: 1200124
This will allow the users name to be displayed without echoing the password to the screen. If you have any questions let me know.

cout << "Please enter your user name: " << flush ;

 cin.getline( szName , max_length ) ;

 cout << "Please enter your password: " << flush ;
 do
 {
      switch( cInput = getch() )
      {
   case enterKey :
      case newLine :
            done = true ;
            break ;

      case backspace :
            if( n )
            {
       --n ;
       szPassword[ n ] = 0x00 ;
       cout << backspace <<' '<<backspace<<flush ;
            }
      break ;

   default :
            szPassword[ n ] = cInput ;
      cout << '*' << flush ;
            if( ++n > max_length )
       done = true ;
            break;
   } ;

 }while( ! done ) ;

0
 
LVL 2

Expert Comment

by:Slarti
ID: 1200125
Of course, that answer requires you to use the getch() function, which is not standard C++. There is no standard C++ method of doing this. The answer depends on your environment: compiler, OS, etc.
0
 
LVL 86

Expert Comment

by:jkr
ID: 1200126
I hardly use cin, but the following code does what you want (using 'stdin'):

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=getch()))
          {  
             *(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
Technology Partners: 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 22

Expert Comment

by:nietod
ID: 1200127
I agree with what you are saying, Slarti, except that the question says "console" and this implies this probably is for a win 32 console program.  Thus the getch() function is likely to be defined.  (Microsoft and Borland do define it.)  
0
 

Author Comment

by:jernhung
ID: 1200128
Actually the code's is supposed to run both under Unix and Windows, which means I need it to be generic C++ where possible. Ansi C is probably good enough.
0
 
LVL 86

Expert Comment

by:jkr
ID: 1200129
In this case, the only change in the above code that is required would be to replace 'getch()' with 'fgetc(stdin)' ;-)
0
 

Author Comment

by:jernhung
ID: 1200130
jkr, bbarnette,

  both of your answers are reasonably good. I can't really tell which is better. So whoever formalises his/her answer gets it :)
  Nevertheless thanks all for your help.




0
 

Author Comment

by:jernhung
ID: 1200131
ah..

 ' .... I can't really tell which is better. So whoever formalises his/her answer gets it :) '
 
  'It' meaning the points, not something sinister. :)


0
 
LVL 1

Expert Comment

by:ylyip
ID: 1200132
I found a C function called "getpass()". You may also try it.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1200133
That is not a standard function.   Some compilers might have it.  Most won't.
0
 
LVL 86

Expert Comment

by:jkr
ID: 1200134
Hmm - what do you mean by 'formalises'?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1200135
i.e "make it formal"--post an answer.
0
 
LVL 86

Accepted Solution

by:
jkr earned 150 total points
ID: 1200136
Thanx, Todd, for the hint ;-)

Thanx also, jernhung!
0

Featured Post

Technology Partners: 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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

662 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