简单实现从控制台记录密码的类

本文介绍了一个简单的C++程序,用于在控制台中安全地输入密码,输入过程中以*显示,并支持Backspace键的功能。

     昨天在逛百度空间时,看到心烦意乱空间的一个c++例子,简单实现从控制台读入密码。无聊就改善之,使之满足

(1)输入密码时,以"*"回显。

(2)支持backspace。

     比较简单...。

 

  1. #include <iostream>
  2. #include <conio.h>//_getch()
  3. #include <string>
  4. #include <cctype>
  5. using namespace std;
  6. class CKeyString
  7. {
  8. private:
  9.     string m_strkey;
  10. public:
  11.     void SetKey();
  12.     bool Range();
  13.     friend istream& operator >> (istream& is,CKeyString * str);
  14.     string ToString() const ;
  15. };
  16. bool CKeyString::Range()
  17. {
  18.     string::size_type n = m_strkey.size();
  19.     return (n < 11 && n > 5);
  20. }
  21. void CKeyString::SetKey()
  22. {
  23.     int n = 0;
  24.     string tempkey;
  25.     while(1)
  26.     {
  27.         if(n)
  28.         cout<<"Input the key again :"<<endl;
  29.         else
  30.         cout<<"Input the key (6b~10b): "<<endl;
  31.         cin>>this;
  32.         n++;
  33.         cout<<endl;
  34.         if(n == 1)
  35.         {
  36.             if(m_strkey.empty())
  37.             {
  38.                 n = 0;
  39.                 cout<<"can't be empty! input again..."<<endl;
  40.             }
  41.             else 
  42.                 if(!Range())
  43.                 {
  44.                     cout<<"the key isn't content..again"<<endl;
  45.                     n = 0;
  46.                 }
  47.                 else 
  48.                     tempkey = m_strkey;
  49.             m_strkey = "";
  50.         }
  51.         else//n == 2
  52.         {
  53.             if(m_strkey == tempkey)
  54.             {
  55.                 cout<<"input succeedly..."<<endl;
  56.                 break;
  57.             }
  58.             cout<<"not the same...again"<<endl; 
  59.             n = 0;
  60.             m_strkey = "";
  61.             tempkey = "";
  62.         }
  63.     }
  64. }
  65. string CKeyString::ToString() const
  66. {
  67.     return m_strkey;
  68. }
  69. istream& operator >> (istream& is,CKeyString *str)
  70. {
  71.     char ch = '/n';
  72.     int size = 0;
  73.     while((ch = _getch()) != 13)
  74.     {
  75.         if(isalnum(ch))
  76.         {//key只可为数字和字母
  77.             str->m_strkey.push_back(ch);
  78.             cout<<"*";
  79.             size++;
  80.         }
  81.         else
  82.         if(ch == '/b')//ch == 8
  83.         {//退格键
  84.             if(str->m_strkey.empty()== false)
  85.             {
  86.                 str->m_strkey.erase(str->m_strkey.erase(str->m_strkey.end()-1));
  87.                 cout<<'/b'<<' '<<'/b';
  88.                 size--;
  89.             }
  90.         }
  91.     }
  92.     return is;
  93. }
  94. //测试
  95. int main()
  96. {
  97.     CKeyString ks;
  98.     ks.SetKey();
  99.     cout<<"the key is: "<<endl;
  100.     cout<<ks.ToString()<<endl;
  101.     return 0;
  102. }

    其中退格实现cout<<'/b'<<' '<<'/b',有点投机取巧。不过不知道如何对控制台的缓冲进行操作,就这样了。

    期待有更好的实现方式....

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值