VOOZH about

URL: https://www.geeksforgeeks.org/cpp/taking-password-as-input-in-cpp/

⇱ Taking password as input in C++ - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Taking password as input in C++

Last Updated : 23 Jul, 2025

There are two methods in which input can be taken in a more secure way:

  • Do not display any content.
  • Display a special character such as an asterisk instead of actual content.

In this method, input content will be invisible. This can be implemented in two ways:

:

Program 1:

Below is the program where console mode is set to enable, echo input, and reset the console mode:

Output: 

👁 Image

: 

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.

Program 2:

Below is the C++ program to demonstrate the use of getch() in conio.h:

Output: 

👁 Image

Drawback: The user can't clear the response made earlier. When backspace is pressed, the input is returned.

Program 3:

Below is the C++ program to demonstrate the solution to the above drawback: 

👁 Image

:

The idea is to use the library <conio.h> here to hide password with asterisk(*). Below is the C++ program using conio.h to hide the password using *:

Program 4: 

Comment