Dear all,

I'm writing a small application in Linux using g++ compiler which asks for username and password. Could you please help me to mask the password entered by the user with * symbol? getch and _getch is not working. Please help.

Thanks,
Ershad

Edited 6 Years Ago by Ershad K: n/a

use getch() function

this function does not echo anything on output string when you type....
put this function in loop and cout '*' after you use getch() in your loop.

i am enclosing a sample....try it out....

#include<iostream.h>
#include<conio.h>
void main()
{
int c=0;
char pass[6];
 char r[6] = "passwd";
 int p;
 cout<<"Password: ";
 for (int i=0;i<6;i++)
 {
  pass[i] = getch();
  cout<<"*";
 }
 for (int j=0;j<6;j++)
  {
    if (pass[j] == r[j])
    c = c+1;
  }
clrscr();
if (c == 6)
    cout<<"Correct Password";
else
    cout<<"Invalid Password";
}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Comments
It worked. Thanks! :)
Oh God, did you actually read the first post?

use getch() function

this function does not echo anything on output string when you type....
put this function in loop and cout '*' after you use getch() in your loop.

i am enclosing a sample....try it out....

#include <iostream.h>
#include <conio.h>

void main()
{
   int c = 0;
   char pass[6];
   char r[6] = "passwd";
   int p;
   cout << "Password: ";
   for ( int i = 0;i < 6;i++ )
   {
      pass[i] = getch();
      cout << "*";
   }
   for ( int j = 0;j < 6;j++ )
   {
      if ( pass[j] == r[j] )
         c = c + 1;
   }
   clrscr();
   if ( c == 6 )
      cout << "Correct Password";
   else
      cout << "Invalid Password";
}

FAIL.

It is running very well on my side......
go through your header files folder....some files may be missing

the program and logic both are very correct...!!

the program and logic both are very correct...!!

You didn't carefully read the first post, the OP asked whether there's a solution available which conforms to the ANSI/ISO C++ standard, since your code uses unportable functions (from conio.h), it doesn't conform to the standard, and thereby is not a good solution since it is of no value for the OP. In addition you defined main() as of type void, this is blatantly wrong, the only return type for main() is int, as defined per standard. For the remainder you didn't use code tags to post your code, and your code uses old-style headers.

Edited 6 Years Ago by mvmalderen: n/a

It is running very well on my side......
go through your header files folder....some files may be missing

the program and logic both are very correct...!!

Wrong, they are NOT correct. Please see the OP's question:
"in Linux using g++ compiler ... getch and _getch is not working."
What part of this did you not understand?

Ershad, if you really want ANSI Standard as your title states, you can't. There is no way to input as you desire in Standard C.

Comments
I too was thinking that there's no standard way to do it, I just didn't dare to post it :$

Thank you everybody, since we have no solution in ANSI standard, let's use 'getch()' :(

This article has been dead for over six months. Start a new discussion instead.