Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading a password from std::cin

People also ask

What is using std :: cin?

std::cin is used to get an input value (cin = character input) << is used with std::cout, and shows the direction that data is moving (if std::cout represents the console, the output data is moving from the variable to the console).

Does CIN work with char?

Cin With Member Functionsget(char &ch): Reads a character from the input and stores it in ch. cin. getline(char *buffer, int length): Reads a stream of characters into the string buffer, stopping when it reaches length-1 characters, an end-of-line character ('n'), or the file's end.

What type is std :: cin?

std::cin. Object of class istream that represents the standard input stream oriented to narrow characters (of type char ). It corresponds to the C stream stdin . The standard input stream is a source of characters determined by the environment.

Does CIN work with string?

Inputting a string You can use cin but the cin object will skip any leading white space (spaces, tabs, line breaks), then start reading when it comes to the first non-whitespace character and then stop reading when it comes to the next white space. In other words, it only reads in one word at a time.


@wrang-wrang answer was really good, but did not fulfill my needs, this is what my final code (which was based on this) look like:

#ifdef WIN32
#include <windows.h>
#else
#include <termios.h>
#include <unistd.h>
#endif

void SetStdinEcho(bool enable = true)
{
#ifdef WIN32
    HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); 
    DWORD mode;
    GetConsoleMode(hStdin, &mode);

    if( !enable )
        mode &= ~ENABLE_ECHO_INPUT;
    else
        mode |= ENABLE_ECHO_INPUT;

    SetConsoleMode(hStdin, mode );

#else
    struct termios tty;
    tcgetattr(STDIN_FILENO, &tty);
    if( !enable )
        tty.c_lflag &= ~ECHO;
    else
        tty.c_lflag |= ECHO;

    (void) tcsetattr(STDIN_FILENO, TCSANOW, &tty);
#endif
}

Sample usage:

#include <iostream>
#include <string>

int main()
{
    SetStdinEcho(false);

    std::string password;
    std::cin >> password;

    SetStdinEcho(true);

    std::cout << password << std::endl;

    return 0;
}

There's nothing in the standard for this.

In unix, you could write some magic bytes depending on the terminal type.

Use getpasswd if it's available.

You can system() /usr/bin/stty -echo to disable echo, and /usr/bin/stty echo to enable it (again, on unix).

This guy explains how to do it without using "stty"; I didn't try it myself.


If you don't care about portability, you can use _getch() in VC.

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

int main()
{
    std::string password;
    char ch;
    const char ENTER = 13;

    std::cout << "enter the password: ";

    while((ch = _getch()) != ENTER)
    {
        password += ch;
        std::cout << '*';
    }
}

There is also getwch() for wide characters. My advice is that you use NCurse which is available in *nix systems also.


Only idea what i have, you could read password char by char, and after it just print backspace ("\b") and maybe '*'.