how to get a password input in c++ console application [duplicate]

只愿长相守 提交于 2019-12-14 03:44:44

问题


what is the technique to get a masked password input as follows:


回答1:


#include <iostream>
#include<windows.h> // for system("pause")
#include<conio.h>  //for getch()

using namespace std;

int main()
{
    char x[10];

    cout<<"enter a password\n";

    for(int i=0; i<10;i++){
        x[i]=getch();
        cout<<"*";

        if(x[i]=='\r') //check if enter key is pressed
            break;

        else if(x[i]=='\b'){

            if(i==0)
                cout<<"\b"<<" "<<"\b";
            else if(i>=1){
                x[i-1]='\0';//make the previous byte null if backspase is pressed
                i=i-2;
                cout<<"\b"<<" "<<"\b\b"<<" "<<"\b";
            }

         }
    }

    cout<<endl<<"the password is  :"<<x<<endl;
    system("pause");
 }


来源:https://stackoverflow.com/questions/13687769/how-to-get-a-password-input-in-c-console-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!