C ++密码掩码 [英] C++ password masking

查看:601
本文介绍了C ++密码掩码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个代码来接收密码输入。下面是我的代码...程序运行良好,但问题是除了数字和字母字符之外的其他键也被读取,例如删除,插入,等等,我可以知道如何避免它? tq ...

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 alphabet
    {
        pw += c;
        cout << "*";
    }
}


推荐答案

使用 isalnum() 用于字母数字或 isalpha()仅用于字母。

此外,您将检查 c == 13 两次,以下内容就足够了。

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.

这篇关于C ++密码掩码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆