登录程序c ++中的密码屏蔽增强功能

时间:2016-06-23 03:33:43

标签: c++ arrays login

几天前,我曾向你们寻求过c ++高级(某种)登录系统的帮助。感谢您的支持,我确实做到了,但现在我希望为该计划添加更多功能。目前,它能够接收用户名字符串,接收密码字符串并屏蔽密码,并将它们与预先设定的值一次又一次地进行比较,直到它们匹配为止。
现在,问题是因为我使用getchar()将数据输入到char数组中,即使按下 Enter 键,它也会将其作为数组的输入。另外,我只能输入最多8个字符的值,之后即使不按 Enter ,它也会自动进入下一步。
我希望修改代码,使其可以将任意数量的字符作为输入,屏蔽它们,然后,当用户按 Enter 时,它会停止采用更多值来填充更多的数组字符。所以基本上,几乎像cin,除了它为每个输入的字符回显'*'。这是我的代码:

#include <iostream.h>  
#include <stdlib.h>  
#include <conio.h>` 

using namespace std;  
int main()  
{
   int i=0;string u,p;char parr[8],ch;  
   while (1)
   { 
 system("cls");
 cout<<"Enter username."<<endl;
 cin>>u;
 system("cls");
 cout<<"Enter password."<<endl;
 for (i=0;i<=7;++i)
 {
     ch=getch();
     parr[i]=ch;
     cout<<'*';
 }

 parr[8]='\0';
 string p="password";

 if (u=="username" && parr==p)
 {
    system("cls");
    cout<<"Welcome!";
    break;
         }

 else
 {
     system("cls");
     cout<<"Username and password entered does not match! Please try again.";
 }
 getch();
   }    
     getch();
}  

1 个答案:

答案 0 :(得分:1)

在Enter后添加条件以退出并将数组扩展为相当大的大小:

char parr[30];

cout << "Enter password." << endl;

for (i = 0; i < 29; ++i)
{
    ch=getch();
    if (ch == '\r')
    {
        parr[i] == '\0';
        break;
    }
    parr[i]=ch;
        cout<<'*';
}
parr[29] = '\0';