-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignup.cpp
93 lines (80 loc) · 2.81 KB
/
signup.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include <fstream>
#include "signup.h"
#include "hash.h"
// Function to check if user ID is valid
bool isUserIdValid(const std::string& userId) {
if (userId.length() < 6 || userId.length() > 10) {
return false;
}
for (char c : userId) {
if (!isalnum(c)) { // Only allow alphanumeric characters
return false;
}
}
return true;
}
// Function to check if password is valid
bool isPasswordValid(const std::string& password) {
if (password.length() < 8 || password.length() > 12) {
return false;
}
bool hasUpper = false, hasDigit = false, hasSymbol = false;
for (char c : password) {
if (isupper(c)) hasUpper = true;
else if (isdigit(c)) hasDigit = true;
else if (c == '@' || c == '$' || c == '#') hasSymbol = true;
}
return hasUpper && hasDigit && hasSymbol;
}
// Function to check if user ID already exists
bool doesUserIdExist(const std::string& userId) {
std::ifstream userFile("user_data.txt");
std::string existingUserId;
while (userFile >> existingUserId) {
if (existingUserId == userId) {
return true;
}
}
return false;
}
// Function to handle the sign-up process
void signUp() {
std::string userId, password;
while (true) {
std::cout << "Enter a User ID (6-10 alphanumeric characters) or type 'exit' to quit: ";
std::cin >> userId;
if (userId == "exit") {
std::cout << "Exiting sign-up process.\n";
return; // Exit the function if the user wants to quit
}
if (!isUserIdValid(userId)) {
std::cout << "Invalid User ID. Please try again.\n";
continue;
}
if (doesUserIdExist(userId)) {
std::cout << "User ID already exists. Please enter a different User ID.\n";
continue;
}
break; // Exit the loop if the User ID is valid and does not exist
}
while (true) {
std::cout << "Enter a Password (8-12 characters, at least one capital letter, one digit, and one symbol @, $, #) or type 'exit' to quit: ";
std::cin >> password;
if (password == "exit") {
std::cout << "Exiting sign-up process.\n";
return; // Exit the function if the user wants to quit
}
if (!isPasswordValid(password)) {
std::cout << "Invalid Password. Please try again.\n";
continue;
}
break; // Exit the loop if the password is valid
}
std::ofstream userFile("user_data.txt", std::ios::app);
userFile << userId << std::endl;
std::ofstream passwordFile("password_data.txt", std::ios::app);
std::string hashedPassword = hashPassword(password);
passwordFile << hashedPassword << std::endl;
std::cout << "Sign-Up Successful!\n";
}