Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hiding user input

Status
Not open for further replies.

raydona

Programmer
May 12, 2005
27
0
0
GB
I am writing a program, console not Windows application, where a password has to be entered and then confirmed. I do not want the user's password to be displayed while it is being entered. In other words when the user enters the character, say, R I wish X to be shown on the screen. How can I go about coding this? I would be very grateful for all help.
 
I don't believe there is a standard or platform-independent way to do this. You might want to look into using getch if that exists on your platform. Here is an example that works with VC++ 7.1:
Code:
#include <iostream>
#include <string>
#include <conio.h>

int main()
{
    std::cout << "Enter password: " << std::flush;
    int c = 0;
    std::string password;
    while (true)
    {
        c = getch();
        if (c == '\r')
        {
            std::cout << std::endl;
            break;
        }

        password += static_cast<char>(c);
        std::cout << '*' << std::flush;
    }
    std::cout << "Your password is: " << password << std::endl;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top