#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct User {
string username;
string password;
};
// Function to register a new user
void registerUser() {
User newUser;
cout << "Enter username: ";
cin >> newUser.username;
cout << "Enter password: ";
cin >> newUser.password;
ofstream file("users.txt", ios::app);
file << newUser.username << ":" << newUser.password << endl;
file.close();
cout << "Registration successful!" << endl;
}
// Function to login an existing user
void loginUser() {
string username, password;
cout << "Enter username: ";
cin >> username;
cout << "Enter password: ";
cin >> password;
ifstream file("users.txt");
string line;
bool found = false;
while (getline(file, line)) {
size_t colonPos = line.find(":");
string storedUsername = line.substr(0, colonPos);
string storedPassword = line.substr(colonPos + 1);
if (username == storedUsername && password == storedPassword) {
found = true;
break;
}
}
file.close();
if (found) {
cout << "Login successful!" << endl;
} else {
cout << "Invalid username or password." << endl;
}
}
int main() {
int choice;
cout << "1. Register" << endl;
cout << "2. Login" << endl;
cout << "Enter your choice: ";
cin >> choice;
if (choice == 1) {
registerUser();
} else if (choice == 2) {
loginUser();
} else {
cout << "Invalid choice." << endl;
}
return 0;
}