-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_management.cpp
75 lines (63 loc) · 2.05 KB
/
user_management.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "user_management.h"
#include <fstream>
#include <iostream>
#include <vector>
#include <sstream>
#include <limits> // for std::numeric_limits
// constructor
User::User() : firstName(""), lastName("") {}
// param constr
User::User(const std::string &first, const std::string &last) : firstName(first), lastName(last) {}
bool User::isValid() const
{
return !firstName.empty() && !lastName.empty();
}
bool registerUser(const std::string &filename, const User &newUser)
{
auto users = readUsers(filename);
// check for existing user
for (const auto &user : users)
{
if (user.firstName == newUser.firstName && user.lastName == newUser.lastName)
{
std::cout << "User " << newUser.firstName << " " << newUser.lastName << " is already registered." << std::endl;
return false;
}
}
users.push_back(newUser);
writeUsers(filename, users);
std::cout << "User registered successfully!" << std::endl;
return true;
}
bool loginUser(const std::string &filename, const std::string &firstName, const std::string &lastName)
{
auto users = readUsers(filename);
for (const auto &user : users)
{
if (user.firstName == firstName && user.lastName == lastName)
{
std::cout << "Login successful! Welcome, " << firstName << " " << lastName << "!" << std::endl;
return true;
}
}
std::cout << "Login failed. User not found." << std::endl;
return false;
}
void displayUserMenu()
{
std::string filename = "users.csv";
std::string first, last;
std::cout << "Enter First Name: ";
std::cin >> first;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // clear input buffer
std::cout << "Enter Last Name: ";
std::cin >> last;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // clear input buffer
User newUser(first, last);
if (!newUser.isValid())
{
std::cerr << "Invalid user data provided." << std::endl;
return;
}
registerUser(filename, newUser);
}