标题:我这个程序的密码部分不知道哪错了
只看楼主
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1728
专家分:3216
注 册:2015-12-2
结帖率:100%
 问题点数:0 回复次数:3 
我这个程序的密码部分不知道哪错了
输入密码,只能擦掉最后一个字符,再按一下退格键程序就死了。
#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;
}
搜索更多相关主题的帖子: public 输入密码 include password 
2016-08-31 11:52
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1728
专家分:3216
注 册:2015-12-2
得分:0 
主要看这一段
                if(ch=='\b')
                {
                    char *p;
                    if(password!="\0")
                    {
                        p=(char *)(&password);
                        cout<<"\b \b";
                        while(*(p+1)!='\0')
                        p++;
                        *p='\0';
                        
                    }

                }
2016-08-31 12:53
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1728
专家分:3216
注 册:2015-12-2
得分:0 
改成这样也不行,提示*(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';
                    }

                }
2016-08-31 13:24
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1728
专家分:3216
注 册:2015-12-2
得分:0 
改成这样
    if(ch=='\b')
                {
                    
                    if(password!="\0")
                    {
                        cout<<"\b \b";
                        password.erase(password.size()-1);
                    }

                }
声明改成 std::string passsword;就好了,谢谢rjsp在上一贴的提点
2016-08-31 13:42



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-468318-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.333965 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved