回显
#include<string.h>
#include <iostream>
using namespace std;
int main()
{
string s="123456";
string s1;
int n=3;
while(true)
{
cout<<"input password"<<endl;
cin>>s1;
if(s1==s)
{
cout<<"Right"<<endl;
break;
}
else
{
cout<<"Wrong"<<endl;
--n;
if(n==0)
break;
}
}
}
不回显
#include <iostream>
#include <string.h>
#include <conio.h>
using namespace std;
string password()
{
string temp;
char c;
cout<<"input password:";
while(true)
{
c=getch();
if(c!=13)//13是键盘上回车键的ASCII值,按下回车键相当于输入一个数值为13的字符
{
temp=temp+c;
cout<<'*';
}
else
break;
}
cout<<endl;//使提示语(Right、Wrong)在下一行输出
return temp;
}
int main()
{
string s="123456";
string s1;
int n=3;
while(true)
{
s1=password();
if(s1==s)
{
cout<<"Right"<<endl;
break;
}
else
{
cout<<"Wrong"<<endl;
--n;//判断次数在主函数里进行
if(n==0)
break;
}
}
}
#include <iostream>
#include<ctime>
#include<cstdlib>
#include<conio.h>
#include<string>
using namespace std;
class Password
{
private:
string p;
public :
void password()
{
cout << "请设置密码" << endl;
char a,b;
string temp,t;
while(true)
{
a=getch();
if(a!=13)
{
if(a==8)
{
int n=t.length();
t=t.erase(n-1);
system("cls");
cout << "请设置密码" << endl;
for(int i=0;i<n-1;++i)
{
cout<<'*';
}
}
else{
t=t+a;
cout<<'*';
}
}
else
{
p=t;
break;
}
}
cout<<endl;
cout << "请输入密码" << endl;
for(int i=0; i<3; ++i)
{
do
{
b = getch();
if(b!=13)
{
temp = temp + b;
cout<<'*';
}
}while(b!=13);//如果是使用do-while循环,就要注意,是先循环再判断所以要在循环中再加一个判断条件进行判断
cout<<endl;
if (temp ==p)
{
cout << "密码正确" << endl;
break;
}
else
{
cout << "请重新输入" << endl;
}
}
}
};
int main()
{
Password p;
p.password();
return 0;
}
#include<iostream>
#include<string.h>
#include<conio.h>
#include<windows.h>
using namespace std;
class Password
{
private:
string p;
public:
void password()
{
cout<<"请设置密码"<<endl;
char c1,c2;
string s1,s2;
c1=getch();
int i;
while(true)
{
c1=getch();
if(c1!=13)
{
if(c1==8&&s1.length()>0)
{
int n=s1.length();
s1=s1.erase(n-1);
system("cls");
cout<<" 请设置密码"<<endl;
for(i=0; i<n-1; ++i)
{
cout<<'*';
}
}
else{
s1=s1+c1;
cout<<'*';
}
}
else
{
p=s1;
break;
}
}
cout<<endl;
for(i=0;i<3;i++)
{
cout<<"请输入密码"<<endl;
c2=getch();
while(c2!=13)
{
if(c2==8&&s2.length()>0)
{
int n=s2.length();
s2.erase(n-1);
system("cls");
cout<<"请输入密码"<<endl;
for(i=0; i<n-1; ++i)
{
cout<<'*';
}
}
else
{
s2=s2+c2;
cout<<'*';
}
c2=getch();
}
cout<<endl;
if(s2==p)
{
cout<<"输入正确"<<endl;
break;
}
else
{
cout<<"请重新输入"<<endl;
}
}
}
};
int main()
{
Password p;
p.password();
return 0;
}