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

Dani AI

Generated

There is no ISO C++ facility to turn off terminal echo or read keystrokes without Enter, so a truly portable solution does not exist. On Linux, the usual approach is to use POSIX termios to disable echo (and optionally canonical mode) and then read characters yourself. The snippet below masks input with asterisks, supports Backspace, and restores the terminal settings even if something goes wrong.

#include <termios.h>
#include <unistd.h>
#include <iostream>
#include <string>

std::string read_password_masked() {
    termios oldt{}, newt{};
    if (tcgetattr(STDIN_FILENO, &oldt) != 0) return {};
    newt = oldt;
    newt.c_lflag &= ~(ECHO | ICANON);      // no echo, read char-by-char
    newt.c_cc[VMIN]  = 1;
    newt.c_cc[VTIME] = 0;
    tcsetattr(STDIN_FILENO, TCSANOW, &newt);

    std::string pwd;
    char ch;
    while (read(STDIN_FILENO, &ch, 1) == 1) {
        if (ch == '\n' || ch == '\r') break;
        if (ch == 127 || ch == 8) {        // handle Backspace on Linux/TTY
            if (!pwd.empty()) { pwd.pop_back(); std::cout << "\b \b" << std::flush; }
        } else {
            pwd.push_back(ch);
            std::cout << "*" << std::flush;
        }
    }
    std::cout << std::endl;
    tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
    return pwd;
}

Notes:

  • Consider RAII or a signal handler to ensure settings are restored on exceptions/signals.
  • Revealing password length with asterisks is a minor leak; many tools disable echo entirely instead.
  • On Unix you can also use ncurses (noecho(); getch();), linking with -lncurses.

See termios(3) man page. The historical getpass() disables echo but is obsolete and removed from POSIX; details in the GNU C Library manual.

Recommended Answers

All 7 Replies

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 Member #46692

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.