Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include "profiles.h"
- #include "prompts.h"
- using namespace std;
- bool loggedIn = false;
- int max_profiles = 5;
- struct base_profile {
- string profile_username;
- string profile_password;
- int profile_id;
- } profile_array[max_profiles];
- void registerProfile(string profile_username, string profile_password) {
- /* Declare Local Variables */
- int i;
- string fileName;
- /* Loop through all profiles in profiles array */
- for (i = 0; i < max_profiles; i++) {
- /* Check if it's an available element in the array */
- if (profile_array[i].profile_id = NULL) {
- /* Initialize Local Variables */
- fileName = profile_username + ".profile";
- ofstream profileFile (fileName.c_str());
- /* Give structure array members the value of the passed paramaters */
- profile_array[i].profile_username = profile_username;
- profile_array[i].profile_password = profile_password;
- profile_array[i].profile_id = i;
- /* Write structure array member data to profile */
- profileFile << profile_array[i].profile_username;
- profileFile << profile_array[i].profile_password;
- profileFile << profile_array[i].profile_id;
- /* Prompt User */
- cout << "Successfully registerd profile: " + profile_array[i].profile_username << endl;
- return;
- /* No null elements ;( */
- } else if (i == max_profiles) {
- cout << "Error: No available profile slots, please delete a profile." << endl;
- return;
- }
- }
- }
- void loginProfile(string profile_username, string profile_password) {
- /* Logged in? */
- if (!loggedIn) {
- /* Not logged in, check if valid profile */
- if (isValidProfile(profile_username, profile_password)) {
- /* Profile valid, continue to login */
- loggedIn = true;
- /* Prompt User */
- cout << "Logged in as: " + profile_username << endl;
- cout << userPrompt;
- return;
- /* Invalid Profile */
- } else {
- /* Prompt User */
- cout << "Error: Invalid username/password combination!" << endl;
- return;
- }
- /* Logged in */
- } else {
- cout << "Error: Profile already logged in, please logout to activate a new profile!" << endl;
- return;
- }
- }
- bool isValidProfile(string profile_username, string profile_password, int profile_id) {
- /* Declare Local Variables */
- int i;
- string fileName;
- /* Loop through profile array */
- for (i = 0; i < max_profiles; i++) {
- /* Check username */
- if (profile_array[i].profile_username == profile_username) {
- /* Valid username, check password */
- if (profile_array[i].profile_password == profile_password) {
- /* Valid password, valid profile credentials */
- return true;
- /* Invalid password */
- } else {
- return false;
- }
- /* Invalid username */
- } else {
- return false;
- }
- }
- }
- void logoutProfile(int profile_id) {
- /* Logged in? */
- if (loggedIn) {
- /* Logged in, so logout */
- loggedIn = false;
- /* Prompt User */
- cout << "Logged out: " << getProfileUsername(profile_id); << endl;
- return;
- /* Not logged in. */
- } else {
- /* Prompt User */
- cout << "Error: No profile currently logged in." << endl;
- return;
- }
- }
- void getProfileUsername(int profile_id) {
- /* Return profile username */
- return profile_array[profile_id].profile_username;
- }
- void getProfilePassword(int profile_id) {
- /* Return profile password */
- return profile_array[profile_id].profile_password;
- }
Add Comment
Please, Sign In to add comment