我这个程序的密码部分不知道哪错了
输入密码,只能擦掉最后一个字符,再按一下退格键程序就死了。#include <iostream>
#include <iomanip>
#include <string>
#include <conio.h>
#include <windows.h>
using namespace std;
class Key
{
public:
int cmp()
{
do
{
cout<<setw(30)<<"请输入密码:"<<endl;
while((ch=getch())!='\r')
{
if(ch=='\b')
{
char *p;
if(password!="\0")
{
p=(char *)(&password);
cout<<"\b \b";
while(*(p+1)!='\0')
p++;
*p='\0';
}
}
else
{
password+=ch;
cout<<"*";
}
}
count++;
if(password==pass)
return 1;
else if(count==3)
{
cout<<"密码输入次数超限,即将退出"<<endl;
Sleep(3000);
exit(0);
}
else
return 0;
}while(count!=3);
}
private:
char ch;
int count=0;
string password;
string pass="eb36520";
};
class Date
{
public:
Date(int a,int b,int c,int d)
{
year=a;
month=b;
day=c;
zh=d;
}
void display()
{
cout<<setw(30)<<year<<"年"<<month<<"月"<<day<<"日第"<<zh<<"周"<<endl;
}
private:
int year;
int month;
int day;
int zh;
};
class Plan:public Date
{
public:
Plan(string a1,string a2,string a3,int m,int n,int o,int p):Date(m,n,o,p)
{
a=a1;
b=a2;
c=a3;
}
void display1()
{
display();
cout<<"事件1:"<<a<<endl;
cout<<"事件2:"<<b<<endl;
cout<<"事件3:"<<c<<endl;
}
private:
string a;
string b;
string c;
};
main()
{
Plan plan{"你好","你在干嘛?","吃饭了吗?",1985,8,5,20};
plan.display1();
Key key;
if(key.cmp())cout<<"********"<<endl;
else cout<<"密码错误"<<endl;
}