How do I hide user input with cin in C++? [duplicate]

Asked
Active3 hr before
Viewed126 times

4 Answers

duplicateinput
90%

I'm trying to make a simple password program so I can get familiar with C++, but I'm having a bit of a problem. In this code, I ask the user for a password they choose, and then they enter it. What I want to code to do is hide the input (not replace it with *s), but still show the cursor, and the text above, before, and after the password is entered, like this:, Is there any kind of biological mechanism that would allow a species with a naturally short lifespan to live longer through cannibalism? , By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy. , Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers

You cannot do this directly using cin. You have to go "lower". Try calling these functions:

#include <termios.h>

...

void HideStdinKeystrokes()
{
    termios tty;

    tcgetattr(STDIN_FILENO, &tty);

    /* we want to disable echo */
    tty.c_lflag &= ~ECHO;

    tcsetattr(STDIN_FILENO, TCSANOW, &tty);
}

void ShowStdinKeystrokes()
{
   termios tty;

    tcgetattr(STDIN_FILENO, &tty);

    /* we want to reenable echo */
    tty.c_lflag |= ECHO;

    tcsetattr(STDIN_FILENO, TCSANOW, &tty);
}
88%

// cin with strings
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string mystr;
  cout << "What's your name? ";
  getline (cin, mystr);
  cout << "Hello " << mystr << ".\n";
  cout << "What is your favorite team? ";
  getline (cin, mystr);
  cout << "I like " << mystr << " too!\n";
  return 0;
}
load more v
72%

For this getch() is used. This function takes a character input from user without buffer and doesn’t wait for the user to press “return” key.,Below is the C++ program to demonstrate the use of getch() in conio.h:,C++ program for hashing with chaining,Below is the C++ program to demonstrate the solution to the above drawback: 

65%

In the above figure, both ‘obj1’ and ‘obj2’ will be having the same input and both the object variables will be pointing to the same memory locations. ,➢ In the above example, N1 and N2 are the two objects. ‘N2’ is the object which stores the value of objec’N1’. ‘N1’ takes 100 as input and will initialise to ‘N2’.  ➢ Both N1 and N2 will have different locations. ,In a deep copy, both objects which have to copy and another which has to be copied will be having different memory locations. ,It is the process of creating a copy of an object by copying data of all the member variables as it is. 

Constructor Program in C++:

#include<iostream> 
using namespace std; 
class ABC { 
 public:ABC(){
    cout<<"Constructor is called automatically";
    cout<<"at the time of execution"<<endl;
 }
 void display(){
        cout<<"hello world";
 } 
}; 

int main() { 
 ABC ob1,ob2;
 ob1.display();
 ob2.display();
 return 0;  
} 
load more v

Other "duplicate-input" queries related to "How do I hide user input with cin in C++? [duplicate]"