please could you answer ... i searched all over but sadly could not find an answer

Dani AI

Generated

is on the right track: standard C++ iostreams don't let you control per-keystroke echoing, so you need platform APIs. Two practical choices are (a) read characters raw and print * yourself (that needs manual handling of backspace and special keys), or (b) turn terminal echo off, let the terminal do line editing, then read a whole line. Option (b) is simpler and less error-prone on POSIX systems.

A compact POSIX pattern (no per-keystroke masking, but keeps backspace/edition working) is to disable ECHO with termios, read with std::getline, then restore the flags:

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

std::string read_password() {
    struct termios oldt, newt;
    tcgetattr(STDIN_FILENO, &oldt);
    newt = oldt;
    newt.c_lflag &= ~ECHO;
    tcsetattr(STDIN_FILENO, TCSANOW, &newt);

    std::string pwd;
    std::getline(std::cin, pwd);

    tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
    std::cout << std::endl;
    return pwd;
}

On Windows the typical approach is _getch/_getche (conio) or the Console API; see the MS docs for details and pitfalls. If you do choose raw per-keystroke masking (printing * as keys arrive), remember to handle backspace, arrow keys and multi-byte sequences yourself.

Security and robustness notes: avoid leaving secrets in std::string if possible — prefer a mutable buffer you can explicitly wipe and use a secure wipe like memset_s (or a vetted library function). Always restore terminal state on exceptions (use an RAII guard) so the user’s terminal isn’t left without echo. For reference: termios details and secure wiping are documented here: termios man pages and .

Recommended Answers

All 2 Replies

It's not really possible with standard C++ I/O. You need more control over keystrokes, and that means using non-standard functions. The most commonly known is getch. getch reads a scan code without echoing it to the screen:

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

int main()
{
  std::string password;
  int ch;

  while ((ch = getch()) != '\r') {
    password += (char)ch;
    std::cout.put('*');
  }

  std::cout << '\n' << password << '\n';
}

But remember that it's totally manual now. If you want to handle stuff like backspaces, your code needs to include logic for it:

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

int main()
{
  std::string password;
  int ch;

  while ((ch = getch()) != '\r') {
    if (ch == '\b') {
      // Remove the character from the string
      password = password.substr(0, password.size() - 1);

      // Remove the mask character too
      std::cout << "\b \b";
    }
    else {
      password += (char)ch;
      std::cout.put('*');
    }
  }

  std::cout << '\n' << password << '\n';
}

thank you soo much

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.