在程序中设定一个密码,待运行后提示用户输入密码,输入密码时以****显示,只有三次输入机会
#include<iostream>
#include<string>
#include<ctime>
#include<cstdlib>
#include<conio.h>
using namespace std;
string password()
{
char c;
string temp;
cout<<"set your password:";
while(true)
{
c=getch();
if(c!=13)
{
temp=temp+c;
cout<<"*";
}
else break;
}
cout<<endl;
return temp;
}
int main()
{
int n=3;
string p="123456";
string s;
while(n!=0)
{
s=password();
if(s==p)
{
cout<<"is right"<<endl;
break;
}
else
{
cout<<"is wrong"<<endl;
}
--n;
if(n==0)cout<<"no chance left!";
}
}