-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
116 lines (105 loc) · 3.44 KB
/
main.cpp
File metadata and controls
116 lines (105 loc) · 3.44 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
#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>
#include <thread>
#include <chrono>
using namespace std;
void loadingAnimation() {
for (int i = 0; i < 5; i++) {
cout << ".";
std::this_thread::sleep_for(std::chrono::milliseconds(500)); // wait 500ms
}
cout << "\n";
}
void registerUser() {
string username, password;
cout << "\x1B[32m"; // set text color to green
cout << "Enter username: ";
cin >> username;
cout << "Enter password: ";
cin >> password;
loadingAnimation();
ifstream file("database.txt");
bool exists = false;
string line;
while (getline(file, line)) {
if (line == username) {
exists = true;
break;
}
}
file.close();
if (exists) {
cout << "\x1B[31m"; // set text color to red
cout << "Username already exists\n";
} else {
ofstream file("database.txt", ios::app);
file << username << "\n" << password << "\n";
file.close();
cout << "\x1B[32m"; // set text color to green
cout << "Registration successful!\n";
}
}
void loginUser() {
string username, password;
cout << "\x1B[32m"; // set text color to green
cout << "Enter username: ";
cin >> username;
cout << "Enter password: ";
cin >> password;
loadingAnimation();
ifstream file("database.txt");
string line;
bool found = false;
while (getline(file, line)) {
if (line == username) {
getline(file, line);
if (line == password) {
found = true;
break;
}
}
}
file.close();
if (found) {
cout << "\x1B[32m"; // set text color to green
cout << "Login successful!\n";
} else {
cout << "\x1B[31m"; // set text color to red
cout << "Invalid username or password\n";
}
}
int main() {
int choice;
cout << "\x1B[34m"; // set text color to blue
cout << "---------------------------------------------------------------------------------------\n";
cout << " __________ ________ _______ _______ \n";
cout << " // \\ // \\ // \\ // \\ \n";
cout << " | Register | | Login | | Exit | | Help | \n";
cout << " \\ // \\ // \\ // \\ // \n";
cout << " __________ _______ _______ _______ \n";
cout << "---------------------------------------------------------------------------------------\n";
cout << "1. Register\n2. Login\n3. Exit\n4. Help\n";
cin >> choice;
if (choice == 1) {
registerUser();
} else if (choice == 2) {
loginUser();
} else if (choice == 3) {
cout << "\x1B[31m"; // set text color to red
cout << "Exiting...\n";
return 0;
} else if (choice == 4) {
cout << "\x1B[34m"; // set text color to blue
cout << "Help menu:\n";
cout << "1. Register: Create a new account\n";
cout << "2. Login: Login to an existing account\n";
cout << "3. Exit: Exit the program\n";
cout << "4. Help: Display this help menu\n";
} else {
cout << "\x1B[31m"; // set text color to red
cout << "Invalid choice\n";
}
return 0;
}