为什么每次输入还没有到5次它就中止了,想了半天想不出来,请大神帮忙
#include<iostream>#include<string>
using namespace std;
int cleckuserinfo(string name,string pwd,int name_len,int pwd_len )
{
int i;
for(i=0;i<name_len;i++)
{
if((name.at(i)=='#')||(name.at(i)=='*')||(name.at(i)=='!'))
{
return 1;
}
}
for(i=0;i<pwd_len;i++)
{
if((pwd.at(i)=='#')||(pwd.at(i)=='*')||(pwd.at(i)=='!'))
{
return 1;
}
}
if((name_len<8)||(name_len>64))
{
return 2;
}
if((pwd_len<8)||(pwd_len>64))
{
return 2;
}
return 0;
}
void errorinfo(int n)
{
if (n==1)
{
cout<<"您输入的用户名或密码含有非法字符#或*或!"<<endl;
}
if (n==2)
{
cout<<"用户名,密码不得少于8个字符,不得多于64个字符!"<<endl;
}
}
void connect(string name,string pwd,int name_len,int pwd_len)
{
string str = "#";
string str1 = name + str + pwd;
cout<<"连接后的新字符串为:"<<str1<<endl;
//从新字符串中提取子串
int m = str1.find(str,0);
string str2,str3;
str2.assign( str1, 0, name_len );
str3.assign( str1, m+1, pwd_len );
if(name_len<=32)
{
str2.append(32-name_len,'*');
}
if(pwd_len<=32)
{
str3.append(32-pwd_len,'*');
}
cout<<"#字符的位置是"<<m<<endl;
cout<<"子串为:"<<endl<<str2<<endl<<str3<<endl;
}
int main (void)
{
cout<<"*********************WELCOME TO THE USER LANDING PLATFORM*********************"<<endl;
string username;
string userpwd ;
cout<<"请输入用户名:"<<endl;
cin>>username;
cout<<"请输入用户密码:"<<endl;
cin>>userpwd;
int namelen=username.length();
int pwdlen =userpwd.length();
int num=1;
int flag=cleckuserinfo(username,userpwd,namelen,pwdlen);
while((flag != 0)&&(num<5))
{
errorinfo(flag);
cout<<"请输入用户名:"<<endl;
cin>>username;
cout<<"请输入用户密码:"<<endl;
cin>>userpwd;
flag=cleckuserinfo(username,userpwd,namelen,pwdlen);
num++;
cout<<"您还有的输入机会为:"<<5-num<<endl;
}
connect(username,userpwd,namelen,pwdlen);
if(flag==0)
{
cout<<"用户登录成功,请稍等"<<endl;
}
else
{
cout<<"非法输入达到5次,程序终止!"<<endl;
}
return 0;
}