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

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";
}
commented: Oh God, did you actually read the first post? -2
commented: It worked. Thanks! :) +1
Member Avatar for iamthwee

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.

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.

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.