Silver
Silver

Reputation: 11

How to delete a character in password masking c++

I'm currently making a login form for my project, but I can't move on on this one. I want my program to delete a character(in password) when I'm pressing the backspace button, but the program is not reading it.

Here's my code

string username, password, power;
char ast=' ';
int aste = 0, accessLevel;
ofstream addAcc;
addAcc.open("Accounts.txt");
cout<<"Username: ";cin.ignore();getline(cin, username);
cout<<"Password:  ";
do
{
    ast = getch();

    if (ast == 13 || ast == ' ' )
    {
        break;
    }
    if(ast==8 || password!="")
    {
        cout<<"\b \b";
        password.erase(password.size()-1);
    }
    cout<<"*";
    password+=ast;
    aste ++;
}while(ast!=13 || ast!=' ');
do
{
    cout<<"Enter Access Level(0-1):  ";cin>>accessLevel;
    switch(accessLevel)
    {
        case 0:
            {
                power = "Cashier";
                break;
            }
        case 1:
            {
                power = "Manager";
                break;
            }
        default: 
            cout<<"Invalid access level."<<endl;
    }
}while(accessLevel>1||accessLevel<0);

Thank you for those who will help.

Upvotes: 1

Views: 2518

Answers (3)

On some platforms getch() will return the DEL character (ascii 127) when you hit BACKSPACE. So change your line

if(ast==8 || password!="")

to

if((ast==8 || ast==127) && !password.empty())

You probably don't want to erase a character everytime that password is not an empty string either, so I removed || password!="" from the condition too. Instead you want to erase one character only if the password is not empty. So you need an && instead of an ||.

Also note, that you're adding the character returned by getch() unconditionally. So if the user enters BACKSPACE, you erase the last character, but append BACKSPACE to the password. So you should only append the character returned by getch() if it was not a BACKSPACE (or DEL).

Thus your code might look like this:

if((ast==8 || ast==127) && !password.empty())
{
    cout<<"\b \b";
    password.erase(password.size()-1);
    aste--;
}
else
{
    password+=ast;
    cout<<"*";
    aste++;
}

Upvotes: 2

do { c = _getch();

        if (c == 13 || c == ' ') // checking ascii key 13 pressed or no
        {
            break;
        }
        if (c == 8 || c == 127)
        {
            if (Password.size() != 0){
                cout << "\b \b";
                Password.erase(Password.size() - 1);
                StarNum--;
            }

        }
        else {
            Password += c;
            cout << "*";
            StarNum++;
        }
    } while (c != 13 || c != ' ');

Upvotes: 0

Ali Mohyudin
Ali Mohyudin

Reputation: 242

Use

#include <conio.h>

and

ch = _getch();

instead ch = getch();

and also change the condition like this

if(ch == 8)
{
    password.erase(password.size()-1);
    aste--;
}else
{
    password +=ch;
    aste++;
}

Upvotes: -1

Related Questions