Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 265
  • Last Modified:

password entry in console application

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
replylalit
Asked:
replylalit
  • 3
  • 3
  • 2
  • +3
1 Solution
 
umangjoshiCommented:
use setattrib() function

or use getch() to get one by one character in a password string
0
 
umangjoshiCommented:
may be setattr() function
0
 
akshayxxCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
ExceterCommented:
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
 
Mayank SAssociate Director - Product EngineeringCommented:
Read the characters using getch () in a loop and keep storing them in an array, and keep displaying '*' for every character entered.

Mayank.
0
 
ExceterCommented:
>> 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
 
akshayxxCommented:
>>>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
 
ExceterCommented:
>> 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
 
gotenksCommented:
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
 
gotenksCommented:
oopps... sorry, didnt notice that it is the same...
0
 
gotenksCommented:
oopps... sorry, didnt notice that it is the same...
0
 
Mayank SAssociate Director - Product EngineeringCommented:
How about rating it now?
0
 
jmcgOwnerCommented:
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!

  • 3
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now