-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
192 lines (160 loc) · 5.15 KB
/
main.cpp
File metadata and controls
192 lines (160 loc) · 5.15 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include "license.h"
#include "tools.h"
int main()
{
bool empty_db = false;
std::fstream aFile, lFile;
std::string input, temp, email;
size_t pos;
aFile.open("accounts.txt", std::fstream::in | std::fstream::app);
lFile.open("licenses.txt", std::fstream::in | std::fstream::app);
while (std::getline(aFile, temp))
{
if (temp.find(':') != std::string::npos) break; else empty_db = true; break;
}
aFile.seekg(0);
// Login/Registration menu
while (true)
{
Tools::clear_console();
std::cout << "Login/Register\n\n 1 -> Login \n 2 -> Register \n 3 -> Exit\n\nOption: ";
std::getline(std::cin, input);
if (input == "1" && empty_db == false)
{
if (!aFile.is_open())
aFile.open("accounts.txt");
bool run_loop = true;
while (run_loop)
{
std::cout << "Enter a new e-mail: ";
std::getline(std::cin, input);
if (Tools::validate_email(input))
{
while (std::getline(aFile, temp))
{
if ((pos = temp.find(':')) != std::string::npos && (email = temp.substr(0, pos)) == input)
{
run_loop = false;
break;
}
}
aFile.clear();
aFile.seekg(0, std::ios::beg);
}
else
std::cerr << "\nInvalid email, please input a valid email address.\n";
}
run_loop = true;
while (run_loop)
{
std::cout << "Enter a new password: ";
std::getline(std::cin, input);
if (Tools::validate_password(input))
{
while (std::getline(aFile, temp))
{
std::cout << temp << std::endl;
if ((pos = temp.find(':')) != std::string::npos && (temp = temp.substr(pos+1)) == input)
{
run_loop = false;
break;
}
}
aFile.clear();
aFile.seekg(0, std::ios::beg);
}
else
std::cerr << "\nInvalid email, please input a valid email address.\n";
}
std::cout << "Logged in successfully!\n";
break;
}
else if (input == "1" && empty_db == true)
{
std::cout << "No active accounts in database, make an account first!\nEnter to continue...";
std::cin.get();
continue;
}
else if (input == "2")
{
aFile.clear();
aFile.seekg(0);
while (true)
{
std::cout << "Enter a new e-mail: ";
std::getline(std::cin, temp);
if (Tools::validate_email(temp))
break;
else
std::cerr << "\nInvalid email, please input a valid email address.\n";
}
while (true)
{
std::cout << "Enter a new password: ";
std::getline(std::cin, input);
if (Tools::validate_password(input))
break;
else
std::cerr << "\nInvalid password, please remove the \':\' from the input.\n";
}
aFile << temp << ":" << input << "\n";
aFile.close();
std::cout << "\nSuccessfully created account! (password -> " << input << ")\n";
std::cout << "\nPress enter to continue...";
std::cin.get();
}
else if (input == "3")
{
exit(0);
}
else
{
std::cerr << "Wrong input, choose either 1 or 2!\n";
}
}
// License menu
if (!Tools::validate_email(email) || !lFile || !aFile)
{
std::cout << "Error encountered creating License object!\n";
exit(-1);
}
License account(email, &lFile, &aFile);
while (true)
{
Tools::clear_console();
Tools::print_menu();
while (true)
{
std::cout << "Option: ";
std::getline(std::cin, input);
if (input.length() > 1)
continue;
switch (input.c_str()[0])
{
case '1':
account.show_licenses();
break;
case '2':
account.create_license();
break;
case '3':
account.delete_license();
break;
case '4':
account.validate_license();
break;
default:
break;
}
}
}
if (aFile.is_open())
aFile.close();
if (lFile.is_open())
lFile.close();
std::cin.get();
}