Advertisement
Guest User

cacat

a guest
Jan 22nd, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <conio.h>
  4. #include <string>
  5. using namespace std;
  6.  
  7. class User {
  8.  
  9. public:
  10.     string username;
  11.     string password;
  12.     string inputUsername;
  13.     string inputPassword;
  14.  
  15.     // User registration
  16.     void userRegisterDo()
  17.     {
  18.  
  19.         ofstream usersFile ("userData.txt");
  20.  
  21.         if (    !usersFile.is_open())
  22.         {
  23.             usersFile.open("userData.txt");
  24.         }
  25.  
  26.             usersFile << username << " " << password << endl;
  27.  
  28.             usersFile.close();
  29.     }
  30.  
  31.     // Initialize user registration
  32.     void userRegister()
  33.     {
  34.         cout << "Welcome!\n-------------------------\n\nPlease register.\nEnter a new username:\n";
  35.         cin >> username;
  36.  
  37.         cout << "\nPlease enter a new password:\n";
  38.         cin >> password;
  39.  
  40.         userRegisterDo();
  41.     }
  42.  
  43.     // User login function
  44.     void login()
  45.     {
  46.         cout << "Please enter your username:\n";
  47.         cin >> inputUsername;
  48.  
  49.         cout << "\nPlease enter your password:\n";
  50.         cin >> inputPassword;
  51.  
  52.         string userAndPass = inputUsername + " " + inputPassword; // Search pattern
  53.         int offset;
  54.         string line;
  55.         ifstream usersFile;
  56.         usersFile.open ("userData.txt");
  57.  
  58.        bool found = false;
  59.         if(usersFile.is_open()) {
  60.           while(getline(usersFile,line) && !found) {
  61.               if (line.compare(userAndPass) == 0) { //match strings exactly!
  62.                   found = true; // found is true => break loop
  63.               }
  64.           }
  65.         usersFile.close(); //close the file before recursivly opening it again later
  66.         if(found) {
  67.         cout << "Welcome "<< inputUsername << '\n';
  68.         MainMenu();
  69.         }
  70.         else {
  71.             cout << "\nUsername and/or password incorrect!\n\n\n\n";
  72.             login(); //ok we didn't find them, lets redue this!
  73.         }
  74.       }
  75.  
  76.         else
  77.             cout << "Unable to open userData.txt file." << endl;
  78.     }
  79.  
  80. };
  81.  
  82. void MainMenu(void){
  83.     cout<<"--------------------------------------------------------"<<endl;
  84.     cout<<endl;
  85.     cout<<"1. Meniu utilizatori"<<endl;
  86.     cout<<endl;
  87.     cout<<"2. Administrare produse"<<endl;
  88.     cout<<endl;
  89.     cout<<"3. Cautare"<<endl;
  90.     cout<<endl;
  91.     cout<<"4. Rapoarte"<<endl;
  92.     cout<<endl;
  93.     cout<<"--------------------------------------------------------"<<endl;
  94. }
  95.  
  96. // Main program
  97. int main() {
  98.  
  99.     User user1;
  100.     ifstream usersFile("userData.txt");
  101.     long begin, end;
  102.  
  103.     if (usersFile.good())
  104.     {
  105.         cout << "File userData.txt found!\n\n";
  106.     }
  107.  
  108.     else {
  109.         user1.userRegister();
  110.     }
  111.  
  112.     if(usersFile.is_open())
  113.     {
  114.         begin = usersFile.tellg();
  115.         usersFile.seekg (0, ios::end);
  116.  
  117.         end = usersFile.tellg();
  118.         usersFile.close();
  119.  
  120.         if(begin == end)
  121.         {
  122.             user1.userRegister();
  123.         }
  124.  
  125.         else
  126.             {
  127.                 user1.login();
  128.             }
  129.     }
  130.  
  131.     getch();
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement