为什么每次输入还没到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;
}
[解决办法]
while((flag != 0)&&(num<=5))
[解决办法]
你输入的值 是什么 我这边可以啊
[解决办法]
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
//---------------------------------------------------------------------------
#pragma argsused
#include<iostream>
#include<string>
using namespace std;
//检查
//如果含特殊字符 1
//长度错误 2
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;
cout<<"共有5次机会"<<endl;
cout<<"******************************************************************************"<<endl;
string username("");
string userpwd("");
int namelen;
int pwdlen;
int num=1;
int flag;
int MAX = 6;
while(num<MAX)
{
cout<<"请输入用户名:"<<endl;
cin>>username;
cout<<"请输入用户密码:"<<endl;
cin>>userpwd;
namelen=username.length();
pwdlen =userpwd.length();
flag=cleckuserinfo(username,userpwd,namelen,pwdlen);
if(flag == 0)
{
break;
}
errorinfo(flag);
num++;
cout<<"您还有的输入机会为:"<<MAX-num<<endl;
}
if(flag==0)
{
cout<<"用户登录成功,请稍等"<<endl;
connect(username,userpwd,namelen,pwdlen);
}
else
{
cout<<"非法输入达到5次,程序终止!"<<endl;
}
system("pause");
return 0;
}