Advertisement
Guest User

Untitled

a guest
Feb 18th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <fstream>
  5. using namespace std;
  6.  
  7. class user
  8. {
  9. friend class db;
  10. string username;
  11. string password;
  12. string email;
  13. public:
  14. user(string u, string p, string e)
  15. {
  16. username = u;
  17. password = p;
  18. email = e;
  19. }
  20. ~user()
  21. {
  22. }
  23. };
  24. class db
  25. {
  26. string dbfilename;
  27. vector<user> users;
  28. public:
  29. ~db()
  30. {
  31.  
  32. }
  33. int find_user(string u)
  34. {
  35. for(int i=0;i<users.size();i++)
  36. {
  37. if(users[i].username == u)
  38. {
  39. return i;
  40. }
  41. }
  42. return -1;
  43. }
  44. bool update()
  45. {
  46. fstream file;
  47. for(int i=0;i<users.size();i++)
  48. {
  49. if(i==0)
  50. {
  51. file.open(dbfilename.c_str(), ios::out);
  52. file << users[i].username << " " << users[i].password << " " << users[i].email << endl;
  53. file.close();
  54. }
  55. else
  56. {
  57. file.open(dbfilename.c_str(), ios::out|ios::app);
  58. file << users[i].username << " " << users[i].password << " " << users[i].email << endl;
  59. file.close();
  60. }
  61. }
  62. return true;
  63. }
  64. bool add_user(string u, string p, string e, int flag)
  65. {
  66. if(!u.empty() && !p.empty() && !p.empty() && find_user(u) == -1)
  67. {
  68. user NewUsr(u,p,e);
  69. users.push_back(NewUsr);
  70. if(flag==1)
  71. {
  72. update();
  73. };
  74. return true;
  75. }
  76. return false;
  77. }
  78. db(string filename)
  79. {
  80. dbfilename=filename;
  81. fstream file;
  82. file.open(dbfilename.c_str());
  83. string cuvant, u, p, e;
  84. for(int i=0;file>>cuvant;i++)
  85. {
  86. u,p,e = "";
  87. if(i == 0)
  88. {
  89. u = cuvant;
  90. }
  91. if(i == 1)
  92. {
  93. p = cuvant;
  94. }
  95. if(i == 2)
  96. {
  97. e = cuvant;
  98. add_user(u,p,e,0);
  99. i=-1;
  100. }
  101. }
  102. file.close();
  103. }
  104. void print()
  105. {
  106. for(int i=0;i<users.size();i++)
  107. {
  108. cout << "[" << i << "]: " \
  109. << users[i].username \
  110. << " " << users[i].password \
  111. << " " << users[i].email << endl;
  112. }
  113. }
  114. bool delete_user(string u)
  115. {
  116. int position = find_user(u);
  117. if(position != -1)
  118. {
  119. users.erase(users.begin()+position);
  120. update();
  121. return true;
  122. }
  123. return false;
  124. }
  125. bool auth(string u, string p)
  126. {
  127. int position = find_user(u);
  128. if(position != -1)
  129. {
  130. if(users[position].password == p)
  131. {
  132. return true;
  133. }
  134. }
  135. return false;
  136. }
  137. };
  138. int main()
  139. {
  140. db DB("Useri");
  141. DB.print();//Listare utilizator
  142. //Login
  143. string u,p,e;
  144. cout << "Login:\n"\
  145. << "User: ";
  146. cin >> u;
  147. cout << "Pass: ";
  148. cin >> p;
  149. if(DB.auth(u,p))
  150. {
  151. cout << "Authentication successful!\n";
  152. }
  153. else
  154. {
  155. cout << "Authentication Failed!\n";
  156. }
  157. //Register.
  158. u = "";p = "";e = "";
  159. cout << "Register new account:\n"\
  160. << "User: ";
  161. cin >> u;
  162. cout << "Pass: ";
  163. cin >> p;
  164. cout << "Mail: ";
  165. cin >> e;
  166. if(DB.add_user(u,p,e,1))
  167. {
  168. cout << "Registration successful!\n";
  169. }
  170. else
  171. {
  172. cout << "Registration Failed!\n";
  173. }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement