C++三次机会验证密码是否正确(输入密码时回显与不回显)(可以用回车键删除的升级版)

本文介绍了使用C++实现密码输入与验证的多种方法,包括回显与非回显方式,以及通过类封装提高代码复用性和安全性。文章详细展示了如何利用标准库和第三方库实现密码的设置、输入及比对功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

回显

#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;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值