用c++编写一个简单的登录系统

编写一个简单登陆系统,拥有登录、注册、退出等简单的功能。

通过编写,根据程序的提示页面进行操作:

具体见下图:

根据窗口提示,进行选择操作。

> 具体代码:#include <iostream>
using namespace std; 
class CDBase
{
public:
CDBase()
{
_phead = new Node();
}
~CDBase()
{
Node *ptr = _phead;
while(ptr != NULL)
{
    _phead = _phead->_pnext;
    delete ptr;
    ptr = _phead;
}
}
bool CheckUserAuth(char *name, char *pwd) //判断用户名、密码是否匹配
{
Node *ptr = _phead->getNext();
while(ptr != NULL)
{
    if((strcmp(name,ptr->_name)==0&&strcmp(pwd,ptr->_pwd)==0))
        return true;
        ptr = ptr->getNext();   
}
return false;
}
bool query(char *name) //名字是否匹配
{
Node *ptr = _phead->getNext();
 while(ptr != NULL)
 {
    if(strcmp(name,ptr->_name)==0)
        return true;
        ptr = ptr->getNext();
 }
 return false;
}
void WriteUserInfo(char *name, char *pwd)   
{
Node *pnode = new Node(name,pwd);
pnode->sexNext(_phead->getNext());
_phead->sexNext(pnode);
}
private:
struct Node
{
Node():_pnext(NULL){}
Node(char *n,char *p):_pnext(NULL)
{
    strcpy(_name,n);
    strcpy(_pwd,p);
}
char _name[20];
char _pwd[20];
Node *_pnext;
void sexNext(Node *p)
{
_pnext = p;
}
Node* getNext()
{
return _pnext;
}
};
Node *_phead;
};

class CGUI
{
public:
CGUI():_running(true){}
void Run();
void ShowMainMenu()       //主页面{
cout<<"****************"<<endl;
cout<<"    1.登录      "<<endl;
cout<<"    2.注册      "<<endl;
cout<<"    3.退出      "<<endl;
cout<<"****************"<<endl;
}
void DoLogin()           //登陆页面
{
cout<<"请输入登录名:";
gets(_name);
cout<<"请输入密码:";
gets(_pwd);
if(_db.CheckUserAuth(_name,_pwd))
{
    cout<<"登陆成功"<<endl;
}
else
{
cout<<"用户名或密码错误!"<<endl;
}
}void DoRegister()            //注册页面 
{
cout<<"欢迎注册"endl;
cout<<"请输入注册名称"<<endl;
gets(_name);
cout<<"请输入密码:"<<endl;
gets(_pwd);
if(_db.query(_name))
{
    cout<<"该用户已经注册过,请重新填写"<<endl;
}
else
{
    _db.WriteUserInfo(_name,_pwd);
    cout<<"用户名"<<_name<<"注册成功"<<endl;
}   
}
void DoExit()                   //退出页面
{
exit(0);
}
private:
char _name[20]; 
char _pwd[20];  
bool _running;
CDBase _db;
};
typedef void (CGUI::*PFUNC)();
typedef struct _TableItem
{
int choice;
PFUNC pfunc;
}TableItem;
TableItem gTable[] = 
{
{1,&CGUI::DoLogin},
{2,&CGUI::DoRegister},
{3,&CGUI::DoExit}
};
int gTablelen = sizeof(gTable)/sizeof(gTable[0]);
void CGUI::Run()
{
int chioce;
while(_running)
{
    ShowMainMenu();
    cout<<"请选择:"endl;
    cin>>chioce;
    getchar();
    //switch语句或for循环都可以,不过后者更适用于公司编写
    /*switch(choice)
    {
    case 1:
        Dologin();
        break;
    case 2:
        DoRegister();
        break;
    case 3:
        DoExit();
        break;
    }*/
    for(int i=0;i   gTablelen;i++)
    {
        if(chioce == gTable[i].choice)
        {
            (this->*gTable[i].pfunc)();
        }
    }
}
}
     int main()
    {
        CGUI gui;
        gui.Run();
    return 0;   
}

这里写图片描述

这里写图片描述

这里写图片描述

ok,第一次写,不太会,就这样吧!

  • 37
    点赞
  • 234
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
一个基本的 C++ 登录系统可以由以下几个步骤组成: 1. 创建一个用户数据库 2. 提示用户输入用户名和密码 3. 验证用户名和密码是否匹配数据库中的信息 4. 如果验证成功,则允许用户访问系统,否则提示用户重新输入用户名和密码。 下面是一个简单的示例代码: ```c++ #include <iostream> #include <string> #include <map> using namespace std; // 定义一个用户类 class User { public: User(string name, string password) { this->name = name; this->password = password; } string getName() { return name; } string getPassword() { return password; } private: string name; string password; }; int main() { // 创建一个用户数据库 map<string, User*> users = { {"user1", new User("user1", "password1")}, {"user2", new User("user2", "password2")}, {"user3", new User("user3", "password3")} }; string username; string password; // 提示用户输入用户名和密码 cout << "Please enter your username: "; cin >> username; cout << "Please enter your password: "; cin >> password; // 验证用户名和密码是否匹配数据库中的信息 if (users.count(username) > 0 && users[username]->getPassword() == password) { cout << "Login successful!" << endl; } else { cout << "Invalid username or password. Please try again." << endl; } return 0; } ``` 在这个示例中,我们使用了一个 `User` 类来表示每个用户,用户信息保存在一个 `map` 中。当用户输入用户名和密码时,我们检查用户名是否存在于数据库中,并且密码是否匹配。如果验证成功,我们输出“Login successful!”,否则输出“Invalid username or password. Please try again.”。 需要注意的是,这个示例只是一个基本的登录系统,它并没有真正的安全性。在实际的系统中,需要使用更加复杂的验证方法来确保用户身份的安全。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值