Advertisement
Guest User

Untitled

a guest
Feb 18th, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.42 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.         int x = users.size();
  36.         for(int i=0;i<x;i++)
  37.         {
  38.             if(users[i].username == u)
  39.             {
  40.                 return i;
  41.             }
  42.         }
  43.         return -1;
  44.     }
  45.     bool add_user(string u, string p, string e)
  46.     {
  47.         if(!u.empty() && !p.empty() && !p.empty() && find_user(u) == -1)
  48.         {
  49.             user NewUsr(u,p,e);
  50.             users.emplace_back(NewUsr);
  51.             users.shrink_to_fit();
  52.             return true;
  53.         }
  54.         return false;
  55.     }
  56.     db(string filename)
  57.     {
  58.         dbfilename=filename;
  59.         fstream file;
  60.         file.open(dbfilename.c_str());
  61.         string cuvant, u, p, e;
  62.         for(int i=0;file>>cuvant;i++)
  63.         {
  64.             u,p,e = "";
  65.             if(i == 0)
  66.             {
  67.                 u = cuvant;
  68.             }
  69.             if(i == 1)
  70.             {
  71.                 p = cuvant;
  72.             }
  73.             if(i == 2)
  74.             {
  75.                 e = cuvant;
  76.                 cout << "Reading user: "<< users.size() << "\n" \
  77.                      << "U: " << u << endl \
  78.                      << "P: " << p << endl \
  79.                      << "E: " << e << endl;
  80.                 add_user(u,p,e);
  81.                 i=-1;
  82.             }
  83.         }
  84.         file.close();
  85.     }
  86.     void print()
  87.     {
  88.         int x = users.size();
  89.         for(int i=0;i<x;i++)
  90.         {
  91.             cout << "[" << i << "]: " \
  92.                  << users[i].username \
  93.                  << " " << users[i].password \
  94.                  << " " << users[i].email << endl;
  95.         }
  96.     }
  97.     bool update()
  98.     {
  99.         fstream file;
  100.         int x = users.size();
  101.         for(int i=0;i<x;i++)
  102.         {
  103.             if(i==0)
  104.             {
  105.                 file.open(dbfilename.c_str(), ios::out);
  106.                 file << users[i].username << " " << users[i].password << " " << users[i].email << endl;
  107.                 file.close();
  108.             }
  109.             else
  110.             {
  111.                 file.open(dbfilename.c_str(), ios::out|ios::app);
  112.                 file << users[i].username << " " << users[i].password << " " << users[i].email << endl;
  113.                 file.close();
  114.             }
  115.         }
  116.         return 0;
  117.     }
  118.     bool delete_user(string u)
  119.     {
  120.         int position = find_user(u);
  121.         if(position != -1)
  122.         {
  123.             users.erase(users.begin()+position);
  124.             update();
  125.             return true;
  126.         }
  127.         return false;
  128.     }
  129. };
  130. int main()
  131. {
  132.     db DB("Useri");
  133.     //DB.print();//Listare utilizator
  134.     //Adaugare utilizator.
  135.     /*
  136.     string u,p,e;
  137.     cout << "Register new account:\n"\
  138.          << "User: ";
  139.     cin >> u;
  140.     cout << "Pass: ";
  141.     cin >> p;
  142.     cout << "Mail: ";
  143.     cin >> e;
  144.     if(DB.add_user(u,p,e))
  145.     {
  146.         cout << "Registration successful!\n";
  147.     }
  148.     else
  149.     {
  150.         cout << "Registration Failed!\n";
  151.     }*/
  152.     //freeing vector
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement