Advertisement
joloos

account program

Feb 2nd, 2024
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <cctype>
  4. #include <string>
  5. #include <cstdlib>
  6. #include <ctime>
  7.  
  8. using namespace std;
  9.  
  10. struct Account{
  11. char id[1000];
  12. char firstname[1000];
  13. char middlename[1000];
  14. char lastname[1000];
  15. char username[1000];
  16. char password[1000];
  17. char mobilenumber[1000];
  18. };
  19.  
  20. //Global
  21. int token = 0;
  22. int index = 0;
  23. Account person[1000];
  24. //Functions
  25. int searchusername();
  26. void signedinmenu();
  27. void signin();
  28. void signup();
  29. void accountmenu();
  30. void magicsquare();
  31. void captcha();
  32.  
  33. int main(){
  34. accountmenu();
  35. }
  36.  
  37. int searchusername(){
  38. char searchusername[1000];
  39. cin>>searchusername;
  40. for(int i = 0; i<1000; i++){
  41.     if(strcmp(searchusername, person[i].username) == 0){
  42.     index = i;
  43.     return 0;
  44.             }
  45.         }
  46.     cout<<endl<<"Username not found";
  47.     return 1;
  48.  
  49.     }
  50.  
  51. void signedinmenu(){ //Signed Into Account Function
  52. while(true){
  53.  
  54. system("cls");
  55. int choice;
  56. cout<<endl<<"[1] Show Profile";
  57. cout<<endl<<"[2] Magic Square Minigame";
  58. cout<<endl<<"[3] Back";
  59. cout<<endl<<"Choice: ";
  60. cin>>choice;
  61. switch(choice){
  62.     case 1:
  63. cout<<endl<<"ID: ";
  64. cout<<person[index].id;
  65.  
  66. cout<<endl<<"First Name: ";
  67. cout<<person[index].firstname;
  68.  
  69. cout<<endl<<"Middle Name: ";
  70. cout<<person[index].middlename;
  71.  
  72. cout<<endl<<"Last Name: ";
  73. cout<<person[index].lastname;
  74.  
  75. cout<<endl<<"Username: ";
  76. cout<<person[index].username;
  77.  
  78. cout<<endl<<"Password: ";
  79. for(int i = 0; i < strlen(person[index].password); i++){
  80.     cout<<"*";
  81. }
  82. cout<<endl<<"Press enter to continue...";
  83. cin.ignore(1000, '\n');
  84. cin.ignore(1000, '\n');
  85. break;
  86.     case 2:
  87. magicsquare();
  88. break;
  89.     case 3:
  90. return;
  91.         }
  92.     }
  93. }  
  94.  
  95. void signin(){ //Sign into Account Function
  96. string inputpassword;
  97. int loginAttempt = 5;
  98. system("cls");
  99.  
  100. cout<<endl<<"SIGN-IN";
  101. while(true){
  102. cout<<endl<<"Username: ";
  103. if(searchusername() == 0)
  104. break;
  105.  
  106. }
  107. while(true){
  108. cout<<endl<<"Password: ";
  109. cin.ignore(1000, '\n');
  110. cin>>inputpassword;
  111. if(inputpassword.compare(person[index].password) == 0)
  112. break;
  113.  
  114. else
  115. loginAttempt--;
  116.  
  117. if(loginAttempt==0){
  118. accountmenu();
  119. }
  120.     }
  121. captcha();
  122. cout<<endl<<"Login Successful";
  123. signedinmenu();
  124.  
  125. }
  126.  
  127.  
  128. void signup(){ //Sign-up Function
  129. string cnfrmpassword;
  130. int passreqsmet = 0;
  131.  
  132. system("cls");
  133. cout<<endl<<"SIGN-UP";
  134. cout<<endl<<"ID: ";
  135. cin.ignore(1000, '\n');
  136. cin>>person[token].id;
  137.  
  138. cout<<endl<<"First Name: ";
  139. cin.ignore(1000, '\n');
  140. cin>>person[token].firstname;
  141.  
  142. cout<<endl<<"Middle Name: ";
  143. cin.ignore(1000, '\n');
  144. cin>>person[token].middlename;
  145.  
  146. cout<<endl<<"Last Name: ";
  147. cin.ignore(1000, '\n');
  148. cin>>person[token].lastname;
  149.  
  150. person[token].firstname[0] = toupper(person[token].firstname[0]);
  151. person[token].middlename[0] = toupper(person[token].middlename[0]);
  152. person[token].lastname[0] = toupper(person[token].lastname[0]);
  153.  
  154. strncat(person[token].username, person[token].firstname, 1);
  155. strncat(person[token].username, person[token].middlename, 1);
  156. strcat(person[token].username, person[token].lastname);
  157. cout<<endl<<"Username: ";
  158. cout<<person[token].username;
  159. cin.ignore(1000, '\n');
  160.  
  161.     while (true) {
  162.         int req1 = 0;
  163.         int req2 = 0;
  164.         int req3 = 0;
  165.         int req4 = 0;
  166.         int req5 = 0;
  167.         person[token].password[0]= '\0';
  168.         cout << endl << "Password must have the following criteria: 1 uppercase, 1 lowercase, 1 digit, 1 special character, and a minimum length of 8.";
  169.         cout << endl << "Password: ";
  170.  
  171.  
  172.  
  173.         cin.getline(person[token].password, sizeof(person[token].password));
  174.  
  175.         if (strlen(person[token].password) >= 8) {
  176.             req1 = 1;
  177.         }
  178.  
  179.         for (int i = 0; i < strlen(person[token].password); i++) {
  180.             if (isupper(person[token].password[i])) {
  181.                 req2 = 1;
  182.                 break;
  183.             }
  184.         }
  185.  
  186.         for (int i = 0; i < strlen(person[token].password); i++) {
  187.             if (islower(person[token].password[i])) {
  188.                 req3 = 1;
  189.                 break;
  190.             }
  191.         }
  192.  
  193.         for (int i = 0; i < strlen(person[token].password); i++) {
  194.             if (isdigit(person[token].password[i])) {
  195.                 req4 = 1;
  196.                 break;
  197.             }
  198.         }
  199.  
  200.         for (int i = 0; i < strlen(person[token].password); i++) {
  201.             if (ispunct(person[token].password[i])) {
  202.                 req5 = 1;
  203.                 break;
  204.             }
  205.         }
  206.  
  207.         if (req1 == 1 && req2 == 1 && req3 == 1 && req4 == 1 && req5 == 1) {
  208.  
  209.             break;
  210.         } else {
  211.             cout << "\nPassword does not meet the criteria. Please try again." << endl;
  212.             continue;
  213.         }
  214. }
  215.  
  216. while(true){
  217. cout<<endl<<"Confirm Password: ";
  218. cin>>cnfrmpassword;
  219. if(cnfrmpassword==person[token].password){
  220. cout << "\nPassword created successfully.";
  221. break;
  222.         }
  223. else{
  224. cout<<endl<<"Incorrect Password";
  225. continue;
  226.         }
  227.     }
  228. while(true){
  229. cout<<endl<<"Mobile No.: ";
  230. cin>>person[token].mobilenumber;
  231. if(strlen(person[token].mobilenumber)!=11){
  232.     cout<<endl<<"Mobile Number must be exactly 11 digits";
  233.     continue;
  234. }
  235. break;
  236.     }
  237. cout<<endl<<"Account created successfully.";
  238.     token++;
  239. }
  240. void accountmenu(){ //Main Menu
  241. int choice;
  242. system("cls");
  243. while(true){
  244. cout<<endl<<"ACCOUNT MENU";
  245. cout<<endl<<"[1] Sign-in";
  246. cout<<endl<<"[2] Sign-up";
  247. cout<<endl<<"[3] Exit";
  248. cout<<endl<<"Choice: ";
  249. cin>>choice;
  250. switch(choice){
  251.     case 1:
  252.         signin();
  253.         continue;
  254.     case 2:
  255.         signup();
  256.         continue;
  257.     case 3:
  258.         return;
  259.         }
  260.     }  
  261. }
  262.  
  263. void magicsquare(){ //Magic Square
  264.     int magicsquare [9];
  265.     cout<<endl;
  266.     cout<<endl<<" Magic Square";
  267.     cout<<endl<<"| 1 | 2 | 3 |";
  268.     cout<<endl<<"| 4 | 5 | 6 |";
  269.     cout<<endl<<"| 7 | 8 | 9 |";
  270.     cout<<endl<<"Enter number from 0 to 9";
  271.     for(int i=0; i<9; i++){
  272.     cout<<endl<<"Enter number for "<<i+1<<": ";
  273.     cin>>magicsquare[i];
  274.     }
  275.     cout<<endl<<" | "<<magicsquare[0]<<" | "<<magicsquare[1]<<" | "<<magicsquare[2]<<" |";
  276.     cout<<endl<<" | "<<magicsquare[3]<<" | "<<magicsquare[4]<<" | "<<magicsquare[5]<<" |";
  277.     cout<<endl<<" | "<<magicsquare[6]<<" | "<<magicsquare[7]<<" | "<<magicsquare[8]<<" |";
  278.    
  279.     /*
  280.     | 0 | 1 | 2 |
  281.     | 3 | 4 | 5 |
  282.     | 6 | 7 | 8 |
  283.     */
  284.    
  285.     int sum1, sum2, sum3, sum4, sum5, sum6, sum7, sum8;
  286.     sum1 = magicsquare[0] + magicsquare[1] + magicsquare[2];
  287.     sum2 = magicsquare[3] + magicsquare[4] + magicsquare[5];
  288.     sum3 = magicsquare[6] + magicsquare[7] + magicsquare[8];
  289.     sum4 = magicsquare[0] + magicsquare[4] + magicsquare[8];
  290.     sum5 = magicsquare[6] + magicsquare[4] + magicsquare[2];
  291.     sum6 = magicsquare[2] + magicsquare[5] + magicsquare[8];
  292.     sum7 = magicsquare[1] + magicsquare[4] + magicsquare[7];
  293.     sum8 = magicsquare[0] + magicsquare[3] + magicsquare[6];
  294.    
  295.     if(sum1 == sum2 && sum2 == sum3 && sum3 == sum4 && sum4 == sum5 && sum5 == sum6 && sum6 == sum7 && sum7 == sum8){
  296.         cout<<endl<<"It is a magic square";
  297.         cout<<endl<<"Press enter to proceed...";
  298.         cin.ignore(1000, '\n');
  299.         cin.ignore(1000, '\n');
  300.         return;
  301.         }
  302.         else
  303.         cout<<endl<<"It is not a magic square";
  304.         cout<<endl<<"Press enter to proceed...";
  305.         cin.ignore(1000, '\n');
  306.         cin.ignore(1000, '\n');
  307.         return;
  308. }
  309.  
  310. void captcha(){
  311.  
  312. int attempt=5;
  313. while(true){
  314. const string characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  315. int charactersLength = characters.length();
  316. int captchaLength = 6;
  317. string randomstring;
  318. string usercaptcha;
  319.  
  320.  
  321. srand(static_cast<unsigned int>(time(nullptr)));
  322.  
  323. for(int i= 0; i<captchaLength; i++){
  324. randomstring += characters[rand() % charactersLength]; 
  325.     }
  326. cout <<endl << "Captcha: " << randomstring << endl;
  327. cout<<endl<<"Enter Captcha: ";
  328. cin>>usercaptcha;
  329.  
  330. if(usercaptcha==randomstring)
  331. break;
  332.  
  333. attempt--;
  334.  
  335. if(attempt==0){
  336. cout<<endl<<"Too many attempts...";
  337. accountmenu();
  338.     }
  339. }
  340.    
  341. }
  342.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement