注册 登录
编程论坛 C++教室

我这个程序的密码部分不知道哪错了

ehszt 发布于 2016-08-31 11:52, 2020 次点击
输入密码,只能擦掉最后一个字符,再按一下退格键程序就死了。
#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;
}
3 回复
#2
ehszt2016-08-31 12:53
主要看这一段
                if(ch=='\b')
                {
                    char *p;
                    if(password!="\0")
                    {
                        p=(char *)(&password);
                        cout<<"\b \b";
                        while(*(p+1)!='\0')
                        p++;
                        *p='\0';
                        
                    }

                }
#3
ehszt2016-08-31 13:24
改成这样也不行,提示*(p+i)为只读
                if(ch=='\b')
                {
                    if(password!="\0")
                    {
                        const char * p=password.c_str();
                        cout<<"\b \b";
                                        for(int i=0;*(p+i+1)!='\0';i++)
                        *(p+i)='\0';
                    }

                }
#4
ehszt2016-08-31 13:42
改成这样
    if(ch=='\b')
                {
                    
                    if(password!="\0")
                    {
                        cout<<"\b \b";
                        password.erase(password.size()-1);
                    }

                }
声明改成 std::string passsword;就好了,谢谢rjsp在上一贴的提点
1