//话不多说,上代码!!!
#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;
}
09-02
470
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-29
2730
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
09-03
368
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-31
1859
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-29
473
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)