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

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.