-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
153 lines (111 loc) · 3.22 KB
/
Copy pathmain.cpp
File metadata and controls
153 lines (111 loc) · 3.22 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <iostream>
#include <fstream>
#include <string>
#include <limits>
using namespace std;
const string USER_FILE = "users.txt";
// Check whether username already exists
bool usernameExists(const string& username) {
ifstream file(USER_FILE);
string storedUsername;
string storedPassword;
while (file >> storedUsername >> storedPassword) {
if (storedUsername == username) {
return true;
}
}
return false;
}
// Register a new user
void registerUser() {
string username;
string password;
cout << "\n========== USER REGISTRATION ==========\n";
cout << "Enter username: ";
cin >> username;
if (username.empty()) {
cout << "Error: Username cannot be empty.\n";
return;
}
if (usernameExists(username)) {
cout << "Error: Username already exists.\n";
return;
}
cout << "Enter password: ";
cin >> password;
if (password.empty()) {
cout << "Error: Password cannot be empty.\n";
return;
}
ofstream file(USER_FILE, ios::app);
if (!file) {
cout << "Error: Unable to open user database.\n";
return;
}
file << username << " " << password << "\n";
file.close();
cout << "Registration successful!\n";
}
// Login an existing user
void loginUser() {
string username;
string password;
cout << "\n============== USER LOGIN ==============\n";
cout << "Enter username: ";
cin >> username;
cout << "Enter password: ";
cin >> password;
ifstream file(USER_FILE);
if (!file) {
cout << "No registered users found. Please register first.\n";
return;
}
string storedUsername;
string storedPassword;
while (file >> storedUsername >> storedPassword) {
if (storedUsername == username &&
storedPassword == password) {
cout << "Login successful! Welcome, "
<< username << ".\n";
file.close();
return;
}
}
file.close();
cout << "Login failed: Invalid username or password.\n";
}
// Main menu
int main() {
int choice;
cout << "============================================\n";
cout << " CODEALPHA LOGIN & REGISTRATION \n";
cout << "============================================\n";
while (true) {
cout << "\n--------------- MAIN MENU ----------------\n";
cout << "1. Register\n";
cout << "2. Login\n";
cout << "3. Exit\n";
cout << "--------------------------------------------\n";
cout << "Enter your choice: ";
if (!(cin >> choice)) {
cout << "Invalid input! Please enter a number.\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
continue;
}
switch (choice) {
case 1:
registerUser();
break;
case 2:
loginUser();
break;
case 3:
cout << "\nThank you for using the system!\n";
cout << "Goodbye!\n";
return 0;
default:
cout << "Invalid choice! Please select 1, 2, or 3.\n";
}
}
}