|
1 |
| -// Autor : Nemonet TYP |
| 1 | +// Autor : OM ROY |
2 | 2 | // Title: A Login and Registration System Programmed in C++
|
3 | 3 | // PROJECT FOR C/C++ PROGRAMMING TUTORIAL
|
4 | 4 |
|
5 |
| - |
6 | 5 | #include <iostream>
|
7 | 6 | #include <fstream>
|
8 |
| -#include <string.h> |
9 |
| -#include <vector> |
10 |
| -#include <functional> |
11 |
| -#include <cmath> |
12 |
| -#include <cstdlib> |
| 7 | +#include <string> |
13 | 8 | #include <ctime>
|
14 |
| -#include "login.cpp" |
| 9 | +#include <random> |
15 | 10 | using namespace std;
|
16 |
| - |
17 |
| -int main() |
| 11 | +bool checkCaptcha(string& captcha, string& user_captcha) |
18 | 12 | {
|
19 |
| - login userLogin; |
20 |
| - string userChoice; |
21 |
| - cout << "\t\t\t_____________________________________________\n\n\n"; |
22 |
| - cout << "\t\t\t Welcome to the NEMO 2023 Login! \n\n"; |
23 |
| - cout << "\t\t\t_________ Menu __________\n\n"; |
24 |
| - cout << "\t | Press 1 to LOGIN |" << endl; |
25 |
| - cout << "\t | Press 2 to REGISTER |" << endl; |
26 |
| - cout << "\t | Press 3 if you forgot PASSWORD |" << endl; |
27 |
| - cout << "\t | Press 4 to EXIT |" << endl; |
28 |
| - cout << "\n\t\t\t Please Enter your choice: "; |
29 |
| - cin >> userChoice; |
30 |
| - cout << endl; |
31 | 13 |
|
32 |
| - if (userChoice == "1") |
33 |
| - { |
34 |
| - userLogin.Login(); |
35 |
| - main(); |
36 |
| - } |
37 |
| - else if (userChoice == "2") |
38 |
| - { |
39 |
| - userLogin.Registration(); |
40 |
| - main(); |
41 |
| - } |
42 |
| - else if (userChoice == "3") |
43 |
| - { |
44 |
| - userLogin.ForgotPassword(); |
45 |
| - main(); |
46 |
| - } |
47 |
| - else if (userChoice == "4") |
48 |
| - { |
49 |
| - cout << "\t\t\t Goodbye! \n\n"; |
50 |
| - } |
| 14 | + return captcha.compare(user_captcha) == 0; |
| 15 | +} |
| 16 | +string generateCaptcha(int n) |
| 17 | +{ |
| 18 | + time_t t; |
| 19 | + srand((unsigned)time(&t)); |
| 20 | + const char* chrs = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; |
| 21 | + string captcha = ""; |
| 22 | + while (n--) |
| 23 | + captcha.push_back(chrs[rand() % 62]); |
| 24 | + return captcha; |
| 25 | +} |
| 26 | +void registeruser() { |
| 27 | + string u, p; |
| 28 | + cout << "Enter username: "; |
| 29 | + cin >> u; |
| 30 | + cout << "Enter password: "; |
| 31 | + cin >> p; |
| 32 | + ofstream outfile("users.txt", ios::app); |
| 33 | + outfile << u << " " << p<< endl; |
| 34 | + outfile.close(); |
| 35 | + cout << "Registration successful!" << endl; |
| 36 | +} |
| 37 | +bool login() { |
| 38 | + string u, p, su, sp;//su:-storedusername,sp:-storedpassword |
| 39 | + cout << "Enter username: "; |
| 40 | + cin >> u; |
| 41 | + cout << "Enter password: "; |
| 42 | + cin >> p; |
| 43 | + string captcha = generateCaptcha(9); |
| 44 | + cout << captcha; |
| 45 | + string usr_captcha; |
| 46 | + cout << "Enter above CAPTCHA: "; |
| 47 | + cin >> usr_captcha; |
| 48 | + if (checkCaptcha(captcha, usr_captcha)) |
| 49 | + printf("\nCAPTCHA Matched\n"); |
51 | 50 | else
|
52 |
| - { |
53 |
| - system("cls"); |
54 |
| - cout << "\t\t\t Please select from the options above\n"; |
55 |
| - main(); |
| 51 | + printf("\nCAPTCHA Not Matched\n"); |
| 52 | + ifstream infile("users.txt"); |
| 53 | + while (infile >> su >>sp) { |
| 54 | + if (su == u && sp == p) { |
| 55 | + infile.close(); |
| 56 | + return true; |
| 57 | + } |
56 | 58 | }
|
| 59 | + infile.close(); |
| 60 | + return false; |
57 | 61 | }
|
| 62 | + |
| 63 | +int main() { |
| 64 | + int c; |
| 65 | + cout << "Enter your choice(1.Registration,2.Login): "; |
| 66 | + cin >> c; |
| 67 | + if (c == 1) { |
| 68 | + registeruser(); |
| 69 | + } |
| 70 | + else if (c == 2) { |
| 71 | + if (login()) { |
| 72 | + cout << "Login successful!" << endl; |
| 73 | + } |
| 74 | + else { |
| 75 | + cout << "Login failed!" << endl; |
| 76 | + } |
| 77 | + } |
| 78 | + else { |
| 79 | + cout << "Invalid !" << endl; |
| 80 | + } |
| 81 | + return 0; |
| 82 | +} |
| 83 | + |
0 commit comments