开发者

C++ password masking

开发者 https://www.devze.com 2022-12-24 13:58 出处:网络
i\'m writing a code to receive password input. Below is my code... the program run well but the problem is other keys beside than numerical and alphabet characters also being read, for example delete,

i'm writing a code to receive password input. Below is my code... the program run well but the problem is other keys beside than numerical and alphabet characters also being read, for example delete, insert, and etc. can i know how can i avoid it? tq...

string pw="";
char c=' ';

while(c != 13) //Loop until 'Enter' is pressed
{
    c = _getch();
    if(c==13)
        break;

    if(c==8)
    {
        if(pw.size()!=0)   //delete only if there is input 
        {
            cout<<"\b \b";
            pw.erase(pw.size()-1);
        }
    }

    if((c>47&&c<58)||(c>64&&c<91)||(c>96&&c<123))  //ASCii code for integer and alpha开发者_C百科bet
    {
        pw += c;
        cout << "*";
    }
}


Filter using isalnum() for alpha-numeric or isalpha() for only alphabets.

Also, you are checking c == 13 twice, following will suffice.

while(1){
  //
  if(c == 13)
    break;
  //
}

if( isalnum(c) ){
  // 'c' is acceptable
}

Some assertion is failing during execution which throws that error.


If you have access to it, you are much better off using the GNU getpass function.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号