Advertisement
Guest User

Log System

a guest
Dec 25th, 2016
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.75 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <vector>
  3. #include <iostream>
  4. #include <fstream>
  5. #include <string>
  6. #include <stdio.h>
  7. #include "functions.h"
  8.  
  9.  
  10. using namespace std;
  11.  
  12. const int SIZE = 100;   // Always use MORE than required
  13. string Usernames[SIZE]; // From the database
  14. string Password[SIZE];  // From the database
  15.  
  16. int GetNameIndex(string query, int size)
  17. {
  18.     for (int i=0; i<size; i++) //Prevents the program from reading garbage
  19.     {
  20.         if (query == Usernames[i]) return i; //Return the index
  21.     }
  22.     return -1; //Error code
  23. }
  24. bool PasswordMatches(int index, string passwd)
  25. {
  26.     return (passwd == Password[index]);
  27. }
  28.  
  29.  
  30.  
  31. int main(int argc, char** argv) {  
  32.     initUserCheck(); // Checks to make sure that a user is registered, if not creates a new one.
  33.    
  34.     //Starts the Login Process.
  35.     //Read the database:
  36.        
  37.         const char *appdata = std::getenv ( "APPDATA" );
  38.         std::string path = appdata;
  39.         path += "\\myprogram\\files\\database.dtbf";
  40.         std::ifstream fin ( path.c_str() );
  41.        
  42.     int i=0;
  43.  
  44.     while (!fin.eof()){
  45.         fin >> Usernames[i] >> Password[i];
  46.         i++; //This represents the number of lines we could extract from the database
  47.     }
  48.  
  49.         //Begin VISUAL login
  50.         RetryLogin:
  51.     string usrname, passwd;
  52.     printf("Username: ");
  53.     cin >> usrname;
  54.  
  55.     int index = GetNameIndex(usrname, i); //Send the database size to this function.
  56.  
  57.     printf("Password: ");
  58.     cin >> passwd;
  59.                        
  60.     if (!PasswordMatches(index, passwd)){ // If Password+Username doesn't match.
  61.    
  62.             printf("Correct Username, incorrect Password.  Please try again.  \n");
  63.             system("PAUSE");
  64.             goto RetryLogin;
  65.     }
  66.    
  67.     fin.close();
  68.     printf("You have successfully logged in.\n\n");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement