Coders Packet

Secure Login and Registration System in C++

By Aishwarya Soni

  • loginforA/
  • This C++ system is an implementation of basically four features for a user: (i)Login (ii)Register (iii)Retrieval of forgotten username OR password (iv)Exit system.

    In this C++ Secure Login and Registration System, the user has to first register with a new username and password, and then he/she will be asked few security questions, in case they forget their password or username, and then all these user data are stored in a file(.txt file), for retrieving these, the user has to correctly enter at least one of the username or password and all security questions’ answers correctly. All these functions are implemented in C++ only, and any C++ IDE will give the desired outputs.

    The following are the main features of the system: (i)Login (ii)Register (iii)Forgot username OR password (iv)Exit system.

    The system is made using the C++ language only, so all you need to get started with is a C++ IDE, The IDEs most popularly used are Code Blocks and Visual Studio Code. Note: You can select any IDE you like.

    This system uses the standard 'iostream' library, which provides the 'cin' and 'cout' methods to read from standard input and write to standard output, respectively, and 'fstream' library, data type object, which represents the file stream generally, and it has the capabilities of performing functions of both 'ofstream' (this represents output file stream and is basically used for creating files and for writing information to files) and 'ifstream' (this represents the input file stream and is basically used for reading the information from the written files) which means it can read information from files, write information to files, and create files. Classes like 'fstream' have objects which maintain a filebuf object as their internal stream buffer, and this basically performs input/output operations on the file they are belonging to (if any).

    How the Functions/Features are implemented:

    Below is an overview of the basic steps followed for building this system. Note: The entire code of the system will also be discussed.

    (I) Register: This function of the system just stores a new user's credentials and this is done by asking for user input and then with the 'ofstream' a file(.txt file is used here) is created and written with the data entered by the user, like in the below example:

    ofstream g("registration.txt");

    Here 'ofstream' is for getting the data stored to the file(named here as registration.txt), and the file does not need to exist or be made beforehand. It is 'ofstream', so, it'll take care of it for you..but please be sure that if there is a file called "registration.txt" in the same folder as the .exe file, the contents will be deleted/overwritten. It is worth noting after creation of the 'ofstream' data type object file 'registration.txt', the user input is taken with the 'iostream' input/output functions: 'getline()/cin' and 'cout'. And then these inputs are stored in the file created('registration.txt') with the help of  'g' here. 

     

    (II) Login: This function of the system is for taking a user's inputs, with the help of input/output functions:'getline()/cin' and 'cout', And then comparing these values with the already stored data of the user(while registration process), For this, we need to read/fetch the stored data in a file using 'ifstream', like in the below example:  

    ifstream f("registration.txt");

    Here 'ifstream' is for getting the data from the file('registration.txt'), and let us assume we've already created a file called "registration.txt". The values, entered by the user, are then compared with all the fetched value(s) stored in the file.

     

    (III) Forgot username OR password: This function of the system is a bit like the login function, It also reads the values of the file ('registration.txt') using 'ifstream' like in the  below example:

    ifstream f("registration.txt");

    And checks, if it matches the user inputs, except for it, asks the user to enter different inputs: Either of the username OR password, they remember, and all the security questions' answers, the user had kept/entered during registering. This function then compares the respective values with those stored in the file for a user and determines if the answers/inputs entered are correct or not and retrieve the user's username OR password if they match with those files' values.

     

    (IV) Exit: This function of the system just closes the system.

     

    Below is the code of the system:

    #include
    #include
    
    using namespace std;
    int main()
    {
        string
        usercommand, //to choose what user wants to do each time
        name, password, quesb, quesa,  quesc,//the ones that will be found in the file(used a file named' registration.txt')
        inName, inPassword,inquesa,inquesb,inquesc, //the ones  user is giving in input from keyboard while verifications
        regquesb,regquesa, regquesc, registerName, registerPassword; //add also what user is  giving in input while registering
    
        while (1)
        {   cout<<"******************************************************************************************************\n\n";
            cout<<"                          Welcome to the Secure C++ login and registration page                   \n\n\n";
            cout<<"                                           Login Securely !!                                   \n\n\n";
            cout<<"  ****************************************     OPTIONS:     ******************************************\n\n";
    
            cout<<"1.LOGIN"<<endl;
            cout<<"2.REGISTER"<<endl;
            cout<<"3.FORGOT PASSWORD (or) USERNAME"<<endl;
            cout<<"4.Exit"<<endl;
            cout<<"\nEnter your choice :";
    
            getline(cin,usercommand);
    
            if (usercommand=="4")
            {
                return 1;
            }
            if (usercommand=="2")
            {
                //open a file for registration
                ofstream g("registration.txt"); //'ofstream' is  for getting the stored data from the file,
                //and the file does not need to have to exist or be made beforehand. It is 'ofstream', so, it'll take care of it for you!
                //but please be sure that if there is a file called "registration.txt" in the same folder as the
                //.exe file, the contents will be deleted/ overwritten
    
                if (!g.is_open()) //if it's not open, then there is no such file with the given name inside this folder (means, in the folder where the .exe file is going to be made )
                {
                    cout<<"could not open file registration.txt\n"; //just so we can know why it ain't working if it doesn't
                    return 0;
                }
                cout<<"\n\n\n" // newlines
                    <<"New Username: ";
                getline(cin, registerName); //user input from keyboard will go into registerName variable for registration
                cout<<"New Password: ";
                getline(cin, registerPassword); //user input from keyboard will go into registerPassword variable fors registration
                cout<<"\nFor Your security we ask you a question ,and for helping you if you ever forget your password   : ";
                cout<<"\n Security Q1: What is Your favorite color? Please enter everything in  lowercase :";
                cout<<"\nEnter the security question's answer:";
                getline(cin,regquesa);
                cout<<"\n Security Q2: What is your contact no: ?:";
                cout<<"\nEnter the security question's answer:";
                getline(cin, regquesb);
                cout<<"\n Security Q3: Where have you completed your high school ,(also city  and state)Please enter everything in  lowercase :";
                cout<<"\nEnter the security question's answer:";
                getline(cin,regquesc);
    
                g<<registerName; //this put whatever's to the right (registerName) into , g (("registration.txt"))
    
                g<<'\n'; //and change to anew line in file
                g<<registerPassword; //and now write/store,the password
                //all data placed  safely insidea the file that g opened
                g<<'\n';
                g<<regquesa;
                g<<'\n';
                g<<regquesb;
                g<<'\n';
                g<<regquesc;
                g<<'\n';
    
                g.close(); //always make sure to close the file, or else we might have to deal with some nasty unwanted stuff in the memory
    
                cout<<"\nRegistration is ..Successful!\n";
            }
            if (usercommand== "1")
            {
                //open the file, and then put the name and password into the strings
                ifstream f("registration.txt"); //'ifstream' is   for getting the data from the file, and
                //let us assume we've already created a file called "registration.txt"
                if (!f.is_open()) //if file is not open, then there is no such file with the given name inside
                //the folder (that is, in this folder where the .exe file is going to be made)
                {
                    cout<<"could not open file\n"; //just so we know why it won't work if it doesn't at any instance
                    return 0;
                }
                getline(f, name, '\n'); //reads the user name  from file f (which is using "registration.txt" for data storing)
                getline(f, password, '\n'); //reads the password from file f (which is using "registration.txt" for data storing)
                //also, we are here telling the file to get the  text up until '\n', here ishow  we  know it reads the whole line at most, and won't go any further
                //and that is done by the 3rd parameter '\n'
                f.close(); //it is not required to open now, since you have the name and password from the file
    
                //login
                while (1)
                {
                    //you are going to input the name and password here
                    cout<<"\n\n\n"
                        <<"Enter Username: ";
                    getline(cin, inName);
                    cout<<"\nEnter Password: ";
                    getline(cin, inPassword);
    
                    if (inName==name && inPassword==password)
                    {
                        cout<<"\nLogin Successful\n" //the '\n' is a character, so that's why I can add it
                        //and it will automatically output a newline in console, alongside the string
                            <<"Welcome, "
                            <<inName;
                        break; //just exit the while loop if you've entered the valid account
                    }
                    cout<<"incorrect name or password\n"; //if user  entered the wrong account details ,
                    //then the while loop is not done yet. So that's why this output is without condition
                }
                //now do something about the account
            }
    
             if (usercommand== "3") //(for C strings) if (!strcmp(usercommand, "forgots password or "))
            {
    
                string ch;
                 //open file, and then put the name and password into the strings
                ifstream f("registration.txt"); //'ifstream' is the one for getting data from the file, and
                //let us assume you've already created a file called "registration.txt"
                if (!f.is_open()) //if it's not open, then there is no such file with the given name inside
                //the folder (that is, in the folder where the .exe file is going to be)
                {
                    cout<<"could not open file\n"; //just so that you know why it won't work if it doesn't
                    return 0;
                }
                getline(f, name, '\n'); //reads the user name from file f (which is using "registration.txt")
                getline(f, password, '\n'); //reads the password from file f (which is using "registration.txt")
                getline(f, quesa, '\n');
                getline(f, quesb, '\n');
                getline(f, quesc, '\n');
                //also, if you tell the file to get you that text up until '\n', that's when you know it reads
                //the whole line at most, and won't go any further
                //and that is done by the 3rd parameter
                f.close(); //you don't need it open now, since we now have the name and password from the file
                cout<<"Forgotten ? We're here for help for retrieving\n";
                cout<<"1.Search your id by username you remember"<<endl;
                cout<<"2.Search your id by password you remember"<<endl;
                cout<<"3.Come to Main menu"<<endl;
                cout<<"Enter your choice :";
                getline(cin,ch);
                if (ch == "1"){
    
                          //forgot
                            while (1)
                            {
                                //user has to input the name and  question's answers here
    
                                cout<<"\n\n\n"
                                <<"Enter Username: ";
                                getline(cin, inName);
    
    
                                cout<<"\n Security Q1: What is Your favorite color? Please enter everything in  lowercase";
                                cout<<"\nPlease Enter the security question's answer:";
                                getline(cin, inquesa);
    
                                cout<<"\n Security Q2: What is your contact no: ?:";
                                cout<<"\nEnter the security question's answer:";
                                getline(cin, inquesb);
    
                                cout<<"\n Security Q3: Where have you completed your high school ,(also specify city  and state)Please enter everything in  lowercase :";
                                cout<<"\nEnter the security question's answer:";
                                getline(cin, inquesc);
    
                                if (inName==name && inquesb==quesb && inquesc==quesc && inquesa==quesa )
                                {
                                    cout<<"\nFinding and Searching  Successful\n" //the '\n' is a character, so we can add it and it will then automatically output a newline in the console, alongside the string
                                        <<"Welcome, "
                                        <<inName;
                                    cout<<"\nYour retrieved password is:" <<password;
                                    break; //this will  exit the while loop if user  entered a valid / correct account credentials
                                }
                                cout<<"incorrect username or security answers\n"; //if the user entered wrong  account details then the while loop is not done yet and should be iterated again till
                                //the correct details are filled in. So this 'cout' is outside  without any 'if' condition
                            }
                            //now process the account
    
                    }
                    if (ch == "2")
                    {
    
                            //forgot
                            while (1)
                            {
                                //user will input the name and password here
                                cout<<"\n\n\nEnter Password: ";
                                getline(cin, inPassword);
    
                                cout<<"\n Security Q1: What is Your favorite color? Please enter everything in  lowercase :";
                                cout<<"\nPlease Enter the security question's answer:";
                                getline(cin, inquesa);
    
                                cout<<"\n Security Q2: What is your contact no: ?:";
                                cout<<"\nEnter the security question's answer:";
                                getline(cin, inquesb);
    
                                cout<<"\n Security Q3: Where have you completed your high school ,(also specify city  and state)Please enter everything in  lowercase :";
                                cout<<"\nEnter the security question's answer:";
                                getline(cin, inquesc);
    
                                if (inPassword==password && inquesb==quesb && inquesc==quesc && inquesa==quesa )
                                {
                                    cout<<"\nFinding and Searching  Successful\n" //the '\n' is a character, so we can  add it and it will then automatically output a newline in the console, alongside the string
                                        <<"Welcome, "
                                        <<name;
                                    cout<<"\nYour Username is:" <<name;
                                    break; //we have to exit the whole while loop if user has entered the valid account
                                }
                                cout<<"incorrect password or security answers\n"; //if the user entered the wrong account            details, then then the while loop is not done yet. So here we have this output "cout .."  without any 'if' condition
                            }
                    }
    
                     if (ch == "3")
                    {
    
                            main();
                    }
    
            }
    
                //now process about the account
    
            cout<<"\n\n\n\n\n"; //give it 5 newlines
        }
        return 1;
    }
    

    In any new project, the goal should always be to learn and understand the concepts of programming, i.e. the principles such as memory-management, object-oriented programming (OOPs), compiling, linking, etc. The goal should not be to get the results as fast as possible, as it interferes with creating good codes. C++, in a way, forces us to create clean solutions and care for memory or otherwise the game just won’t work. This is the benefit of working with purely compiled languages as the compiler tells you to do it correctly or things will crash. This could be hard in the beginning but we get the opportunity to learn a lot of stuff in a short time.

    This system helps the developers to know these important concepts of programming and to understand how to code, like using standard functions for input/output and storing valuable data to a file, and using this data when needed in order to perform any function/needs of the system. The important feature used were: storing input data to a file('.txt') using, 'fstream' header, this is for handling and managing data and files, apart from it, the other useful features used is 'iostream' header, this is for storing and getting user's inputs. 

    Hope this packet helped you in someway : )

     

     

    Download Complete Code

    Comments

    Comments for the packet "Secure Login and Registration System in C++";
    No comments yet