25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

LogInSystem.cpp 4.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * Written by : Lee Bin Hong
  3. * Last edited : 4/12/2016
  4. */
  5. #include <iostream>
  6. #include <string>
  7. #include <fstream>
  8. #include "Person.cpp"
  9. using namespace std;
  10. int getUsernameCounter(string, int);
  11. void login();
  12. void loggedIn();
  13. void registration();
  14. void editCredentials();
  15. void chgUsername();
  16. void chgPassword();
  17. void chgEmail();
  18. void chgPhoneNo();
  19. void quit();
  20. const int size = 10000;
  21. Person person[size];
  22. string username[size];
  23. string password[size];
  24. string email[size];
  25. string phoneNo[size];
  26. int counter;
  27. int x = 0;
  28. int wrongPass = 0;
  29. int main()
  30. {
  31. ifstream fin("database.txt");
  32. int choice;
  33. while (!fin.eof())
  34. {
  35. fin >> username[x] >> password[x] >> email[x] >> phoneNo[x];
  36. x++;
  37. }
  38. cout << "Please choose one of the following options :" << endl;
  39. cout << "Log In - 1" << endl;
  40. cout << "Registration - 2" << endl;
  41. cout << "Exit - 0" << endl;
  42. cin >> choice;
  43. switch (choice)
  44. {
  45. case 1:
  46. login();
  47. break;
  48. case 2:
  49. registration();
  50. break;
  51. case 0:
  52. break;
  53. }
  54. ofstream fout("database.txt");
  55. for (int i = 0; i <= x; i++)
  56. {
  57. fout << username[i] << " " << password[i] << " " << email[i] << " " << phoneNo[i] << " " << endl;
  58. }
  59. return 0;
  60. }
  61. int getUsernameCounter(string query, int size)
  62. {
  63. for (int i = 0; i < size; i++)
  64. {
  65. if (query == username[i]) return i;
  66. }
  67. return -1;
  68. }
  69. void login()
  70. {
  71. if (wrongPass >= 3)
  72. {
  73. cout << "Too much failed login attempt. The program will now be terminated." << endl;
  74. quit();
  75. }
  76. string usrnme, pswd;
  77. cout << "Username:";
  78. cin >> usrnme;
  79. counter = getUsernameCounter(usrnme, x);
  80. cout << "Password:";
  81. cin >> pswd;
  82. if (pswd != password[counter])
  83. {
  84. cout << "Invalid username or password. Please try again.";
  85. wrongPass++;
  86. login();
  87. }
  88. loggedIn();
  89. }
  90. void registration()
  91. {
  92. string usrnme;
  93. string pswd = "2";
  94. string pswd2 = "3";
  95. bool available = false;
  96. while (available == false)
  97. {
  98. available = true;
  99. cout << "Username : ";
  100. cin >> usrnme;
  101. for (int i = 0; i < x; i++)
  102. {
  103. if (username[i] == usrnme)
  104. {
  105. cout << "Username unavailable. Please try again." << endl;
  106. available = false;
  107. }
  108. }
  109. }
  110. cout << "Username is available." << endl;
  111. username[x] = usrnme;
  112. while (pswd != pswd2)
  113. {
  114. cout << "Password : ";
  115. cin >> pswd;
  116. cout << "Confirm password :";
  117. cin >> pswd2;
  118. if (pswd != pswd2)
  119. cout << "Password unmatched. Please try again.";
  120. }
  121. password[x] = pswd;
  122. cout << "Email : ";
  123. cin >> email[x];
  124. cout << "Phone No. : ";
  125. cin >> phoneNo[x];
  126. cout << "Account is successfully registered." << endl;
  127. counter = x;
  128. x++;
  129. loggedIn();
  130. }
  131. void loggedIn()
  132. {
  133. int choice;
  134. cout << "Please choose one of the following option :" << endl;
  135. cout << "View credentials - 1" << endl;
  136. cout << "Edit credentials - 2" << endl;
  137. cout << "Exit - 0" << endl;
  138. cin >> choice;
  139. switch (choice)
  140. {
  141. case 1:
  142. cout << "Username : " << username[counter] << "\nEmail : " << email[counter] << "\nPhone No. : " << phoneNo[counter] << endl;
  143. loggedIn();
  144. break;
  145. case 2:
  146. editCredentials();
  147. loggedIn();
  148. break;
  149. case 0:
  150. quit();
  151. }
  152. }
  153. void editCredentials()
  154. {
  155. int choice;
  156. cout << "Which of the following to edit?" << endl;
  157. cout << "Username - 1" << endl;
  158. cout << "Password - 2" << endl;
  159. cout << "Email - 3" << endl;
  160. cout << "Phone No. - 4" << endl;
  161. cout << "Exit - 0" << endl;
  162. cin >> choice;
  163. switch (choice)
  164. {
  165. case 1:
  166. chgUsername();
  167. break;
  168. case 2:
  169. chgPassword();
  170. break;
  171. case 3:
  172. chgEmail();
  173. break;
  174. case 4:
  175. chgPhoneNo();
  176. break;
  177. }
  178. }
  179. void chgUsername()
  180. {
  181. bool available = false;
  182. string usrnme;
  183. while (available == false)
  184. {
  185. available = true;
  186. cout << "Username : ";
  187. cin >> usrnme;
  188. for (int i = 0; i < x; i++)
  189. {
  190. if (username[i] == usrnme)
  191. {
  192. cout << "Username unavailable. Please try again." << endl;
  193. available = false;
  194. }
  195. }
  196. }
  197. username[counter] = usrnme;
  198. loggedIn();
  199. }
  200. void chgPassword()
  201. {
  202. string pass;
  203. cout << "Please input the current password : ";
  204. cin >> pass;
  205. if (pass != password[counter])
  206. {
  207. cout << "Wrong password. Please try again.";
  208. wrongPass++;
  209. chgPassword();
  210. }
  211. }
  212. void chgEmail()
  213. {
  214. cout << "Please input the new email : ";
  215. cin >> email[counter];
  216. loggedIn();
  217. }
  218. void chgPhoneNo()
  219. {
  220. cout << "Please input the new phone no. : ";
  221. cin >> phoneNo[counter];
  222. loggedIn();
  223. }
  224. void quit()
  225. {
  226. ofstream fout("database.txt");
  227. for (int i = 0; i <= x; i++)
  228. {
  229. fout << username[i] << " " << password[i] << " " << email[i] << " " << phoneNo[i] << " " << endl;
  230. }
  231. exit (EXIT_SUCCESS);
  232. }