1

I wrote a program that gets the input from user to enter his username and password but what i want is that when user inputs his password it is displayed as ***** and not in english just like when we enter password in Gmail.

I researched on internet and i have managed to write this program but i can't input anything in the password.Also i dont fully understand the code that i have written.

class user
{
private:
   string name;
   string pass;
public:
                void mask()
{
        char c;
        for(int i=0;i<100;i++)
        {
                c=getch();
                if(c=='\r')
                        break;
                std::cout<<"*";
                pass=pass+c;
        }
}
        void getdata()
        {
                int flag=0;
                do
                {
                cout<<"Enter username: ";std::getline(std::cin,name);
                cout<<endl;
                cout<<"Enter password: ";mask();
                cout<<endl;
                 if((name=="myname")&&(pass=="tiger"))
                        {
                                cout<<"\n\nLogin successful"<<endl;
                                flag=1;
                        }
                else
                        cout<<"\n\nInvalid username or password\n\nHint:Your username and password is myname and tiger"<<endl<<endl;
        }while(flag==0);
        }
};
Leonardo Alves Machado
  • 2,747
  • 10
  • 38
  • 53
suyashsingh234
  • 189
  • 1
  • 2
  • 12

1 Answers1

0

question has been asked, regardless here is my implementation It only accepts valid input (numbers and letter can be easily changed) and it supports backspace

#include <iostream>
#include <string>
#include <windows.h>

using namespace std;

int main()
{
    string password = "";
    while (!GetAsyncKeyState(VK_RETURN) & 1)
    {
        for (int i = 0x30; i < 0x5A; i++)
        {
            if (GetAsyncKeyState(i) & 1)
            {
                if (i >= 0x41 && i <= 0x5A && ((GetKeyState(VK_CAPITAL) & 1) == 0 || GetAsyncKeyState(VK_SHIFT) & 1))
                    password += ((char)i + 32);
                else
                    password += (char)i;

                cout << "*";
                Sleep(50);
            }
            else if (GetAsyncKeyState(VK_BACK) & 1)
            {
                password.erase(password.size() - 1);
                system("cls");
                for (int i = 0; i < password.size(); i++)
                {
                    cout << '*';
                }
                Sleep(50);
            }
        }
    }
    cout << password;
    Sleep(10000);
}
iZeusify
  • 140
  • 1
  • 7