C++ | Mask the password

Hello peeps! Lord Hypersonic greets you. Today I am presenting you a C++ program to hide password. You have seen this feature when you enter the password on any website. And if you are a beginner program you must be thinking how you can make such thing in C++. So no worries now. I will explain to you how to do it and will provide you the source code to play with it.



I have made a function of this so that you can use this function without any difficulties.













This function will return a string which contains the string which the user enters and it will also display "@" in place of the actual characters the user is pressing.

Now lets us understand the algorithm of this program.

Algorithm

For making this program you need 3 variables (2 strings, and 1 character).
One string will be used to print "@" and the second string will be used to store the characters which the user is pressing.
The character variable will be used to take input from the keyboard.
For taking input we are going to use _getch() function from conio.h header file.

About _getch() function

This function is used to take character input from the keyboard and do not print that character.

So we are going to use this feature of the function. 

First, declare the three variables string Minput, print; char input;

Now print "Enter password" or anything of your choice.
Then ask for the input ( input  = _getch() ).

now start while loop till user press enter. How to know that? From ASCII table we know that the ASCII code of Enter key / Return Key is 13. So, we will declare loop as while (input != 13) 

After declaring loop, check wheater user press Backspace key or not (ASCII code = 8). 
If user press backspace key then checks the length of the print or Minput.

If the length of print is zero then make print and Minput equal to an empty string.
If the length is not equal to zero then resize both strings (i.e., Minput and print) to there length - 1. And print the value of string input.

If the user is not pressing Backspace, instead he is pressing any other key then add "@" at end of print ( print.push_back('@') ) and add the character entered by the user at the end of Minput (Minput.push_back(input) ).

After checking conditions ask user for input again ( input = _getsh() ).

Clear the screen.

Print "Enter password: " or whatever you want.

After the loop is terminated return the value of Minput, which stores the input provided by the user.

To understand more clearly read the source code given below.

Source Code

/*
Program: masked password
Description: To hide the password/ input and display @ in place of the character inputted.
Author: Lord Hypersonic
Email: lordhypersonic.522@gmail.com
Website: www.lordhypersonic.blogspot.com
*/
#include <iostream>
#include <string>
#include <conio.h>
#include <stdlib.h>

using namespace std;

string masked_input()
{
    string Minput,print;
    char input;
    cout<<"Enter password: ";
    input = _getch();
    while (input != 13) // continue till user press RETURN/ENTER Key
    {
        if (input == 8) // if user press backspace key
        {
            if (print.size() == 0) // if length of Minput is zero
            {
                print = ""; //make print equal to empty string
                Minput = ""; //make Minput equal to empty string
            }
            else //if length of Minput/print is not zero or greater than zero
            {
                print.resize(print.size()-1); //resize print to its length - 1
                cout<<print; // print value of print
                Minput.resize(Minput.size()-1); // resize Minput string to its length - 1
            }
        }
        else // if user do not press backspace key.
        {
            print.push_back('@'); //add @ at end of print
            cout<<print; //print value of print
            Minput.push_back(input); //add the key pressed at the end of Minput string
        }
        input = _getch(); //ask user for input / press keys
        system("cls"); //clear screen
        cout<<"Enter password: ";
    }
    return Minput;
}

int main()
{
    string password = masked_input();

    if (password == "m@$k3d p@$$w0rd") //if password entered is correct
    {
        cout<<endl<<"Welcome back my friend"<<endl; //print this
    }
    else  //if password is not correct
    {
        cout<<endl<<"Incorrect password........... Can't let you in"<<endl; //print this
    }
    return 0;
}

ENJOY 😉

No comments

Powered by Blogger.