You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LogInSystem.cpp 4.4 KiB

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