?
Solved

password entry in console application

Posted on 2003-03-05
14
Medium Priority
?
260 Views
Last Modified: 2010-04-15
Dear all,
I am developing a console based application and it requires a password entry field. I want that password should not be printed in screen (rather some character should be displayed instead)
eg.
Enter password : *****
Please comment!
0
Comment
Question by:replylalit
[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
  • 3
  • 3
  • 2
  • +3
14 Comments
 
LVL 1

Expert Comment

by:umangjoshi
ID: 8070726
use setattrib() function

or use getch() to get one by one character in a password string
0
 
LVL 1

Expert Comment

by:umangjoshi
ID: 8070731
may be setattr() function
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8070809
this  works on windows... on linux u need slightly special stuff

#include<stdio.h>
#include<conio.h>

void main()
{
int a,i;
char c,buf[32];

printf(" input password.... ");
i=0;
while((c=getch())!='\r'){
     printf("%c",'*');
buf[i++]=c;
}
buf[i]='\0';
printf("\nu entered password %s\n",buf);
}
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 8

Accepted Solution

by:
Exceter earned 200 total points
ID: 8071179
That setup works perfectly except for the fact that it does not support the backspace key. Try this,

#include <stdio.h>
#include <conio.h>
#define STRLEN 25

void input( char*, int );

int main()
{
    char s[STRLEN];
    printf("Enter password: ");
    input(s,STRLEN);
       printf("\n\n%s\n",s);
    return 0;
}

void input( char* buffer, int length )
{
  int pos = 0;
  char c;

  buffer[0] = '\0';

  while( ( c = getch() ) != 13 )
  {
      if( c != 8 && pos < ( length - 1 ) )
      {
          printf("*");
          buffer[pos] = c;
          pos++;
      }
      else if( pos != 0 && c == 8 )
      {
          printf("%c %c",8,8);
          pos--;
      }
  }
   buffer[pos] = '\0';
}

Exceter
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8101138
Read the characters using getch () in a loop and keep storing them in an array, and keep displaying '*' for every character entered.

Mayank.
0
 
LVL 8

Expert Comment

by:Exceter
ID: 8103614
>> Read the characters using getch () in a loop and keep storing them in an array, and keep displaying '*' for every character entered.

That is EXACTLY what the previously posted code does. :-)

Exceter
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8103672
>>>That is EXACTLY what the previously posted code does. :-)

and  the prior to that one too :P

strange stats .. today most of the notifications that came to me..80 % were comment from Mayankeagle..
and many of them were similar like this one ... repeated ideas..
0
 
LVL 8

Expert Comment

by:Exceter
ID: 8114735
>> today most of the notifications that came to me..80 % were comment from Mayankeagle..
>> and many of them were similar like this one ... repeated ideas..

I noticed that same phenomenon..

Exceter
0
 

Expert Comment

by:gotenks
ID: 8157252
try this :

void input() {
   char c[8];
   int index = 0;

   while (1) {
      c[index] = getch();
      printf("*");
      index++;
      if (index == 8) {
         c[index] = '\0';
         break;
      }    
   }
   
/*   printf("%s\n", c); */
}

this should do the trick, i hope. anyway, it is just a sample that will accept input of 8 characters and exit the while loop.
0
 

Expert Comment

by:gotenks
ID: 8157275
oopps... sorry, didnt notice that it is the same...
0
 

Expert Comment

by:gotenks
ID: 8157282
oopps... sorry, didnt notice that it is the same...
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8544873
How about rating it now?
0
 
LVL 20

Expert Comment

by:jmcg
ID: 10199361
Nothing has happened on this question in more than 8 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer by Exceter.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses
Course of the Month9 days, 3 hours left to enroll

764 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