c++编QQ

//话不多说,上代码!!!
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
// 模拟用户结构体
struct User {
    std::string username;
    std::string password;
};

// 存储用户信息的向量
std::vector<User> users;

// 注册用户
void registerUser() {
    User newUser;
    cout << "请输入用户名:";
    cin >> newUser.username;
    cout << "请输入密码:";
    cin >> newUser.password;
    users.push_back(newUser);
    ofstream outFile("users.txt", std::ios::app);
    outFile << newUser.username << " " << newUser.password << std::endl;
    outFile.close();
    cout << "注册成功!" << std::endl;
}

// 登录用户
bool loginUser() {
    string username, password;
    cout << "请输入用户名:";
    cin >> username;
    cout << "请输入密码:";
    cin >> password;
    for (const User& user : users) {
        if (user.username == username && user.password == password) {
            return true;
        }
    }
    return false;
}

int main() {
    ifstream inFile("users.txt");
    if (inFile.is_open()) {
        string username, password;
        while (inFile >> username >> password) {
            User user;
            user.username = username;
            user.password = password;
            users.push_back(user);
        }
        inFile.close();
    }

    short choice;
    cout << "1. 注册\n2. 登录\n请选择:";
    cin >> choice;

    if (choice == 1) {
        registerUser();
    } else if (choice == 2) {
        if (loginUser()) {
            cout << "登录成功!" << endl;
        } else {
            cout << "登录失败!" << endl;
        }
    }

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值