Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

LogInSystem.cpp 5.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * Written by : BinHong Lee
  3. * Last edited : 4/12/2016
  4. */
  5. #include <iostream>
  6. #include <string>
  7. #include <stack>
  8. #include <fstream>
  9. #include "Person.cpp"
  10. using namespace std;
  11. int getUsernameCounter(string, int);
  12. void login();
  13. void loggedIn();
  14. void registration();
  15. void editCredentials();
  16. void update(Person);
  17. void chgUsername();
  18. void chgPassword();
  19. void chgEmail();
  20. void chgPhoneNo();
  21. void quit();
  22. stack<Person> users;
  23. Person currentUser;
  24. string username;
  25. string password;
  26. string email;
  27. string phoneNo;
  28. int counter;
  29. int x = 0;
  30. int wrongPass = 0;
  31. int main()
  32. {
  33. ifstream fin("database.txt");
  34. int choice;
  35. while (!fin.eof())
  36. {
  37. fin >> username >> password >> email >> phoneNo;
  38. Person temp(username, password, email, phoneNo);
  39. users.push(temp);
  40. }
  41. users.pop();
  42. cout << "Please choose one of the following options :" << endl;
  43. cout << "Log In - 1" << endl;
  44. cout << "Registration - 2" << endl;
  45. cout << "Exit - 0" << endl;
  46. cin >> choice;
  47. switch (choice)
  48. {
  49. case 1:
  50. login();
  51. break;
  52. case 2:
  53. registration();
  54. break;
  55. case 0:
  56. break;
  57. }
  58. ofstream fout("database.txt");
  59. while (!users.empty())
  60. {
  61. fout << users.top().getName() << " " << users.top().getPassword() << " " << users.top().getEmail() << " " << users.top().getPhoneNo() << " " << endl;
  62. users.pop();
  63. }
  64. return 0;
  65. }
  66. void getUser(string query)
  67. {
  68. stack<Person> temp = users;
  69. while (!temp.empty())
  70. {
  71. Person x = users.top();
  72. if (x.getName() == query) currentUser = x;
  73. users.pop();
  74. cout << "User found";
  75. }
  76. throw std::invalid_argument("");
  77. }
  78. void login()
  79. {
  80. if (wrongPass >= 3)
  81. {
  82. cout << "Too much failed login attempt. The program will now be terminated." << endl;
  83. quit();
  84. }
  85. try {
  86. string usrnme, pswd;
  87. cout << "Username:";
  88. cin >> username;
  89. cout << "Password:";
  90. cin >> password;
  91. getUser(username);
  92. } catch (invalid_argument inae) {
  93. cout << "Invalid username or password. Please try again.";
  94. wrongPass++;
  95. login();
  96. }
  97. if (!currentUser.checkPassword(password))
  98. {
  99. cout << "Invalid username or password. Please try again.";
  100. wrongPass++;
  101. login();
  102. }
  103. loggedIn();
  104. }
  105. void registration()
  106. {
  107. string pswd = "2";
  108. string pswd2 = "3";
  109. bool available = true;
  110. do
  111. {
  112. cout << "Username : ";
  113. cin >> username;
  114. stack<Person> temp = users;
  115. while(!temp.empty())
  116. {
  117. Person currentPerson = temp.top();
  118. if (currentUser.getName() == username)
  119. {
  120. cout << "Username unavailable. Please try again." << endl;
  121. available = false;
  122. break;
  123. }
  124. temp.pop();
  125. }
  126. } while (available == false);
  127. cout << "Username is available." << endl;
  128. do
  129. {
  130. cout << "Password : ";
  131. cin >> pswd;
  132. cout << "Confirm password :";
  133. cin >> pswd2;
  134. if (pswd != pswd2)
  135. cout << "Password unmatched. Please try again.";
  136. } while (pswd != pswd2);
  137. password = pswd;
  138. cout << "Email : ";
  139. cin >> email;
  140. cout << "Phone No. : ";
  141. cin >> phoneNo;
  142. Person newUser(username, password, email, phoneNo);
  143. users.push(newUser);
  144. cout << "Account is successfully registered." << endl;
  145. currentUser = newUser;
  146. loggedIn();
  147. }
  148. void loggedIn()
  149. {
  150. int choice;
  151. cout << "Please choose one of the following option :" << endl;
  152. cout << "View credentials - 1" << endl;
  153. cout << "Edit credentials - 2" << endl;
  154. cout << "Exit - 0" << endl;
  155. cin >> choice;
  156. switch (choice)
  157. {
  158. case 1:
  159. cout << "Username : " << currentUser.getName() << "\nEmail : " << currentUser.getEmail() << "\nPhone No. : " << currentUser.getPhoneNo() << endl;
  160. loggedIn();
  161. break;
  162. case 2:
  163. editCredentials();
  164. loggedIn();
  165. break;
  166. case 0:
  167. quit();
  168. }
  169. }
  170. void editCredentials()
  171. {
  172. int choice;
  173. cout << "Which of the following to edit?" << endl;
  174. cout << "Username - 1" << endl;
  175. cout << "Password - 2" << endl;
  176. cout << "Email - 3" << endl;
  177. cout << "Phone No. - 4" << endl;
  178. cout << "Exit - 0" << endl;
  179. cin >> choice;
  180. switch (choice)
  181. {
  182. case 1:
  183. chgUsername();
  184. break;
  185. case 2:
  186. chgPassword();
  187. break;
  188. case 3:
  189. chgEmail();
  190. break;
  191. case 4:
  192. chgPhoneNo();
  193. break;
  194. }
  195. }
  196. void update(Person newUser)
  197. {
  198. stack<Person> newUsers;
  199. while (!users.empty())
  200. {
  201. if (users.top().getName() == currentUser.getName())
  202. {
  203. newUsers.push(newUser);
  204. }
  205. else
  206. {
  207. newUsers.push(users.top());
  208. }
  209. users.pop();
  210. }
  211. users = newUsers;
  212. }
  213. void chgUsername()
  214. {
  215. bool available = true;
  216. Person newUsers;
  217. do
  218. {
  219. cout << "Username : ";
  220. cin >> username;
  221. stack<Person> temp = users;
  222. for (int i = 0; i < x; i++)
  223. {
  224. if (temp.top().getName() == username)
  225. {
  226. cout << "Username unavailable. Please try again." << endl;
  227. available = false;
  228. break;
  229. }
  230. temp.pop();
  231. }
  232. } while (available == false);
  233. cout << "Username is available." << endl;
  234. Person newPerson(username, currentUser.getPassword(), currentUser.getEmail(), currentUser.getPhoneNo());
  235. update(newPerson);
  236. cout << "Username is updated." << endl;
  237. }
  238. void chgPassword()
  239. {
  240. cout << "Please input the current password : ";
  241. cin >> password;
  242. while (!currentUser.checkPassword(password))
  243. {
  244. cout << "Wrong password. Please try again.";
  245. wrongPass++;
  246. cout << "Please input the current password : ";
  247. cin >> password;
  248. }
  249. string password2;
  250. do {
  251. cout << "Please input the new password : ";
  252. cin >> password;
  253. cout << "Please confirm your new password : ";
  254. cin >> password2;
  255. } while(password2 != password);
  256. currentUser.setPassword(password);
  257. update(currentUser);
  258. }
  259. void chgEmail()
  260. {
  261. cout << "Please input the new email : ";
  262. cin >> email;
  263. currentUser.setEmail(email);
  264. update(currentUser);
  265. }
  266. void chgPhoneNo()
  267. {
  268. cout << "Please input the new phone no. : ";
  269. cin >> phoneNo;
  270. currentUser.setPhoneNo(phoneNo);
  271. update(currentUser);
  272. }
  273. void quit()
  274. {
  275. ofstream fout("database.txt");
  276. for (int i = 0; i <= x; i++)
  277. {
  278. fout << username[i] << " " << password[i] << " " << email[i] << " " << phoneNo[i] << " " << endl;
  279. }
  280. exit (EXIT_SUCCESS);
  281. }