Guest User

Untitled

a guest
Apr 7th, 2013
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <windows.h>
  4. #include <string>
  5. #include <fstream>
  6. #include "login.h"
  7. using namespace std;
  8.  
  9. login::login() // Constructor
  10. {
  11.     accountusername = "";
  12.     accountpassword = "";
  13. }
  14.  
  15. void login::homeScreen()
  16. {
  17.     system("CLS");
  18.    
  19.     cout << "__________________[HOME]____________________" << endl;
  20.     cout << "-Enter 'Login' to begin login" << endl;
  21.     cout << "-Enter 'Register' to register an account" << endl;
  22.     cout << "-Enter 'ForgotInfo' to recover account info\n" << endl;
  23.     cout << "[";
  24.  
  25.     string homechoice;
  26.     cin >> homechoice;
  27.  
  28.     if(homechoice == "Login" || homechoice == "login")
  29.         loginScreenU();
  30.     else if(homechoice == "Register" || homechoice == "register")
  31.         registerScreenU();
  32.     else if(homechoice == "ForgotInfo" || homechoice == "forgotinfo")
  33.         forgotinfoScreen();
  34.     else
  35.     {
  36.         cout << "- ERROR: Invalid Entry -- Please enter a valid choice." << endl;
  37.         Sleep(1500);
  38.         homeScreen();
  39.     }
  40. }
  41.    
  42. void login::loginScreenU()
  43. {
  44.     system("CLS");
  45.  
  46.     cout << "Username: ";
  47.     string username;
  48.     cin >> username;
  49.  
  50.     verifyUsername(username);
  51.  
  52.     if(verifyUsername(username) == true)
  53.         loginScreenP();
  54.  
  55.     while(verifyUsername(username) == false)
  56.     {
  57.         system("CLS");
  58.         cout << "ERROR: '" << username << "' is an invalid username - Please enter a valid username.\n" << endl;
  59.         cout << "-If you would like to return to the main menu, please enter 'Menu'." << endl;
  60.         cout << "Username: ";
  61.  
  62.         cin >> username;
  63.  
  64.         if(username == "Menu" || username == "menu")
  65.             homeScreen();
  66.     }  
  67. }
  68.  
  69. void login::loginScreenP()
  70. {
  71.     system("CLS");
  72.    
  73.     cout << "Password: ";
  74.     string password;
  75.     cin >> password;
  76.  
  77.     verifyPassword(password);
  78.    
  79.     if(verifyPassword(password) == true)
  80.     {
  81.         system("CLS");
  82.         cout << "Thank you for logging in!" << endl;
  83.     }
  84.     else
  85.     {
  86.        Sleep(1000);
  87.        system("CLS");
  88.        loginScreenP();
  89.     }
  90. }
  91.  
  92. void login::registerScreenU()
  93. {
  94.     system("CLS");
  95.    
  96.     cout << "Thank you for choosing to register. Please fill out the info on screen." << endl;
  97.     registerUsername();
  98. }
  99.  
  100. void login::forgotinfoScreen()
  101. {
  102.     system("CLS");
  103.  
  104.     if(accountusername == "")
  105.     {
  106.         cout << "You have not yet registered an account!" << endl;
  107.         cout << "Returning to Main Menu..." << endl;
  108.         Sleep(1500);
  109.         system("CLS");
  110.         homeScreen();
  111.     }
  112.     else
  113.     {
  114.         cout << "Security Question: " << securityquestion << endl;
  115.         cin >> recoveryanswer;
  116.  
  117.         if(recoveryanswer == securityanswer)
  118.         {
  119.             cout << "\nUsername: " << accountusername << endl;
  120.             cout << "Password: " << accountpassword << endl;
  121.             cout << "\nReturning to Main Menu..." << endl;
  122.             Sleep(2000);
  123.             homeScreen();
  124.         }
  125.         else
  126.         {
  127.             cout << "ERROR: Invalid security question answer!" << endl;
  128.             cout << "Returning to Main Menu..." << endl;
  129.             Sleep(2000);
  130.             homeScreen();
  131.         }
  132.     }
  133. }
  134.  
  135. void login::registerUsername()
  136. {
  137.     cout << "\nUsername: ";
  138.     cin >> accountusername;
  139.  
  140.     if(accountusername.length()<4)
  141.     {
  142.         system("CLS");
  143.         cout << "ERROR: Username must be greater than 4 characters!" << endl;
  144.         registerUsername();
  145.     }
  146.     else if(accountusername.length()>10)
  147.     {
  148.        system("CLS");
  149.        cout << "ERROR: Username must be less than 10 characters!" << endl;
  150.        registerUsername();
  151.     }
  152.     else
  153.     {
  154.         system("CLS");
  155.         registerPassword();
  156.     }
  157. }
  158.  
  159. void login::registerPassword()
  160. {
  161.     cout << "Username: " << accountusername << endl;
  162.     cout << "Password: ";
  163.  
  164.     cin >> accountpassword;
  165.  
  166.     ofstream outputFile("AccountInfo.txt");
  167.     outputFile <<  accountusername << "\n"
  168.                <<  accountpassword << endl;
  169.  
  170.     if(accountpassword.length()<4)
  171.     {
  172.         system("CLS");
  173.         cout << "\nERROR: Password must be greater than 4 characters!" << endl;
  174.         registerPassword();
  175.     }
  176.     else if(accountpassword.length()>10)
  177.     {
  178.        system("CLS");
  179.        cout << "\nERROR: Password must be less than 10 characters!" << endl;
  180.        registerPassword();
  181.     }
  182.     else
  183.     {
  184.         system("CLS");
  185.         cout << "Username: " << accountusername << endl;
  186.         cout << "Password: ";
  187.  
  188.         for(int i = 0; i < accountpassword.length(); i++)
  189.             cout << "*";
  190.  
  191.         cout << "\nConfirm Password: ";
  192.         cin >> confirmpassword;
  193.  
  194.         if(confirmpassword == accountpassword)
  195.         {
  196.             system("CLS");
  197.             cout << "One last step, please select the number for a security question you would like:" << endl;
  198.             securityQuestion();
  199.         }
  200.         else
  201.         {
  202.             cout << "\nERROR: Passwords do not match!" << endl;
  203.             Sleep(1000);
  204.             system("CLS");
  205.             registerPassword();
  206.         }
  207.     }
  208. }
  209.  
  210. void login::securityQuestion()
  211. {
  212.     cout << "1. What is the name of your first pet?" << endl;
  213.     cout << "2. What is your Mother's middle name?" << endl;
  214.     cout << "3. What is your Father's middle name?" << endl;
  215.     cout << "- ";
  216.  
  217.     int choice;
  218.     cin >> choice;
  219.  
  220.     if(choice == 1)
  221.     {
  222.         system("CLS");
  223.         cout << "1. What is the name of your first pet?" << endl;
  224.         securityquestion = "What is the name of your first pet?";
  225.         cin >> securityanswer;
  226.         system("CLS");
  227.         accountCreated();
  228.     }
  229.     else if(choice == 2)
  230.     {
  231.         system("CLS");
  232.         cout << "2. What is your Mother's middle name?" << endl;
  233.         securityquestion = "What is your Mother's middle name?";
  234.         cin >> securityanswer;
  235.         system("CLS");
  236.         accountCreated();
  237.     }
  238.     else if(choice == 3)
  239.     {
  240.         system("CLS");
  241.         cout << "3. What is your Father's middle name?" << endl;
  242.         securityquestion = "What is your Father's middle name?";
  243.         cin >> securityanswer;
  244.         system("CLS");
  245.         accountCreated();
  246.     }
  247.     else
  248.     {
  249.         system("CLS");
  250.         securityQuestion();
  251.     }
  252.  
  253. }
  254.  
  255. void login::accountCreated()
  256. {
  257.     cout << "Thank you for registering!" << endl;
  258.     cout << "Returning to Main Menu..." << endl;
  259.     Sleep(2000);
  260.     system("CLS");
  261.     homeScreen();
  262. }
  263.  
  264. bool login::verifyUsername(string user)
  265. {
  266.     ifstream inputFile("AccountInfo.txt");
  267.     inputFile.open("AccountInfo.txt");
  268.  
  269.     string first_line;
  270.  
  271.     if(inputFile.is_open())
  272.     {
  273.         if(inputFile.good())
  274.             getline(inputFile, first_line);
  275.             inputFile.close();
  276.     }
  277.     else
  278.     {
  279.         cout << "ERROR: Cannot find Account Information!" << endl;
  280.         cout << "Returning to Main Menu..." << endl;
  281.         Sleep(2000);
  282.         system("CLS");
  283.         homeScreen();
  284.     }
  285.    
  286.     if(user == first_line)
  287.         return true;
  288.     else
  289.         return false;
  290. }
  291.  
  292. bool login::verifyPassword(string pass)
  293. {
  294.     if(pass == accountpassword)
  295.         return true;
  296.     else
  297.     {
  298.         cout << "ERROR: Invalid Password!" << endl;
  299.         return false;
  300.     }
  301. }
Advertisement
Add Comment
Please, Sign In to add comment