Guest User

Untitled

a guest
Oct 24th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include "profiles.h"
  4. #include "prompts.h"
  5.  
  6. using namespace std;
  7.  
  8. bool loggedIn = false;
  9. int max_profiles = 5;
  10.  
  11. struct base_profile {
  12.     string profile_username;
  13.     string profile_password;
  14.     int profile_id;
  15. } profile_array[max_profiles];
  16.  
  17. void registerProfile(string profile_username, string profile_password) {
  18.     /* Declare Local Variables */
  19.     int i;
  20.     string fileName;
  21.  
  22.     /* Loop through all profiles in profiles array */
  23.     for (i = 0; i < max_profiles; i++) {
  24.         /* Check if it's an available element in the array */
  25.         if (profile_array[i].profile_id = NULL) {
  26.             /* Initialize Local Variables */
  27.             fileName = profile_username + ".profile";
  28.             ofstream profileFile (fileName.c_str());
  29.  
  30.             /* Give structure array members the value of the passed paramaters */
  31.             profile_array[i].profile_username = profile_username;
  32.             profile_array[i].profile_password = profile_password;
  33.             profile_array[i].profile_id = i;
  34.  
  35.             /* Write structure array member data to profile */
  36.             profileFile << profile_array[i].profile_username;
  37.             profileFile << profile_array[i].profile_password;
  38.             profileFile << profile_array[i].profile_id;
  39.  
  40.             /* Prompt User */
  41.             cout << "Successfully registerd profile: " + profile_array[i].profile_username << endl;
  42.             return;
  43.         /* No null elements ;( */
  44.         } else if (i == max_profiles) {
  45.             cout << "Error: No available profile slots, please delete a profile." << endl;
  46.             return;
  47.         }
  48.     }
  49. }
  50.  
  51. void loginProfile(string profile_username, string profile_password) {
  52.     /* Logged in? */
  53.     if (!loggedIn) {
  54.         /* Not logged in, check if valid profile */
  55.         if (isValidProfile(profile_username, profile_password)) {
  56.             /* Profile valid, continue to login */
  57.             loggedIn = true;
  58.             /* Prompt User */
  59.             cout << "Logged in as: " + profile_username << endl;
  60.             cout << userPrompt;
  61.             return;
  62.         /* Invalid Profile */
  63.         } else {
  64.             /* Prompt User */
  65.             cout << "Error: Invalid username/password combination!" << endl;
  66.             return;
  67.         }
  68.     /* Logged in */
  69.     } else {
  70.         cout << "Error: Profile already logged in, please logout to activate a new profile!" << endl;
  71.         return;
  72.     }
  73. }
  74.  
  75. bool isValidProfile(string profile_username, string profile_password, int profile_id) {
  76.     /* Declare Local Variables */
  77.     int i;
  78.     string fileName;
  79.  
  80.     /* Loop through profile array */
  81.     for (i = 0; i < max_profiles; i++) {
  82.         /* Check username */
  83.         if (profile_array[i].profile_username == profile_username) {
  84.             /* Valid username, check password */
  85.             if (profile_array[i].profile_password == profile_password) {
  86.                 /* Valid password, valid profile credentials */
  87.                 return true;
  88.             /* Invalid password */
  89.             } else {
  90.                 return false;
  91.             }
  92.         /* Invalid username */
  93.         } else {
  94.             return false;
  95.         }
  96.     }
  97. }
  98.  
  99. void logoutProfile(int profile_id) {
  100.     /* Logged in? */
  101.     if (loggedIn) {
  102.         /* Logged in, so logout */
  103.         loggedIn = false;
  104.         /* Prompt User */
  105.         cout << "Logged out: " << getProfileUsername(profile_id); << endl;
  106.         return;
  107.     /* Not logged in. */
  108.     } else {
  109.         /* Prompt User */
  110.         cout << "Error: No profile currently logged in." << endl;
  111.         return;
  112.     }
  113. }
  114.  
  115. void getProfileUsername(int profile_id) {
  116.     /* Return profile username */
  117.     return profile_array[profile_id].profile_username;
  118. }
  119.  
  120. void getProfilePassword(int profile_id) {
  121.     /* Return profile password */
  122.     return profile_array[profile_id].profile_password;
  123. }
Add Comment
Please, Sign In to add comment