0

How to create a input function that doesnt displays what you have entered instead it displays another character? ex- suppose you have entered "hello" it shows nothing or it shows "*****". With a proper backspace functionality. THANKS IN ADVANCE

  • 2
    I don't understand? If you know how to do this, then why are you asking this question? – Geno C Nov 02 '20 at 05:35
  • It's not possible using standard C++. OS specific functionality is needed. And since you don't tell us the OS you're targeting, we can't answer your question. – Some programmer dude Nov 02 '20 at 05:36
  • 1
    @vectorX `conio.h` is not available on linux. – brc-dd Nov 02 '20 at 05:38
  • 1
    Also, this isn't a competition. Please read [the help pages](http://stackoverflow.com/help), especially ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also take the [tour] and read [ask] and [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Nov 02 '20 at 05:38
  • As I said, not possible using **standard** C++. The `conio.h` header file is not a standard C++ header file. It's an old DOS leftover in Windows. – Some programmer dude Nov 02 '20 at 05:41

3 Answers3

0

EDIT: It looks like you already know the answer to your own question? I'll just leave this here in case anyone else finds it useful...

¯\_(ツ)_/¯

I'm going to assume you're wanting to do this in a console application, and you're probably on Windows. If so, I found this solution online from https://www.cplusplus.com/articles/E6vU7k9E/ - maybe give this method a try. The link also has methods for other platforms.

#include <iostream>
#include <string>
#include <windows.h>
using namespace std;


string getpass(const char *prompt, bool show_asterisk=true)
{
  const char BACKSPACE=8;
  const char RETURN=13;

  string password;
  unsigned char ch=0;

  cout <<prompt<<endl;

  DWORD con_mode;
  DWORD dwRead;

  HANDLE hIn=GetStdHandle(STD_INPUT_HANDLE);

  GetConsoleMode( hIn, &con_mode );
  SetConsoleMode( hIn, con_mode & ~(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT) );

  while(ReadConsoleA( hIn, &ch, 1, &dwRead, NULL) && ch !=RETURN)
    {
       if(ch==BACKSPACE)
         {
            if(password.length()!=0)
              {
                 if(show_asterisk)
                     cout <<"\b \b";
                 password.resize(password.length()-1);
              }
         }
       else
         {
             password+=ch;
             if(show_asterisk)
                 cout <<'*';
         }
    }
  cout <<endl;
  return password;
}
Alex
  • 310
  • 1
  • 6
0

There is no standard way to do this in C++.

Different operating systems provide different API to interact with the (virtual) terminal. There is a solution for Linux in this more specific question: https://stackoverflow.com/a/63038844/2079303

eerorika
  • 232,697
  • 12
  • 197
  • 326
0

For windows, using <windows.h>

string passwdIO()
{
    HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); 
    DWORD mode = 0;
    GetConsoleMode(hStdin, &mode);
    SetConsoleMode(hStdin, mode & (~ENABLE_ECHO_INPUT));
    string input;
    getline(cin, input);
    SetConsoleMode(hStdin, mode);
    return input;
}

This is a normal way to do this. This function returns string which is not displayed on screen.

but we can do it using iostream and <conio.h>

string encPasswd(const char _dsp){
    string _final = "";
    char chr_ipt;
    while(true){
        chr_ipt = getch();
        if (chr_ipt < 32 && chr_ipt != 8)
            return _final;
            if(chr_ipt == 8 && _final.length() != 0){
            _final.pop_back();
            cout << "\b \b";
            continue;
        }
        if(chr_ipt == 8 && _final.length() == 0) continue; 
        // without this^ program will crash at some point

        _final.push_back(chr_ipt);
        cout << _dsp;
    }
}

this function returns string while taking input but showing the another character (_dsp)