【问题标题】:Password masking (input displayed as ********)密码屏蔽(输入显示为 ********)
【发布时间】:2018-04-23 15:06:51
【问题描述】:

我编写了一个程序,它从用户那里获取输入以输入他的用户名和密码,但我想要的是当用户输入他的密码时,它显示为 ***** 而不是英文,就像我们在输入密码时一样邮箱。

我在互联网上研究并设法编写了这个程序,但我无法在密码中输入任何内容。而且我不完全理解我编写的代码。

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);
        }
};

【问题讨论】:

标签: c++ passwords masking


【解决方案1】:

问题已经问过了,不管这是我的实现它只接受有效的输入(数字和字母可以很容易地改变)并且它支持退格

#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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-02
    • 2017-12-20
    • 1970-01-01
    • 1970-01-01
    • 2015-06-30
    • 2011-12-29
    • 2017-04-30
    • 2011-02-01
    相关资源
    最近更新 更多