Providing Username and Password to your application in C++



Ever wanted to provide a Username and Password to the application that you've created so that not everyone could run the program? If YES! Then your wait is over! In this post you'll get know how to do just that :)


STEP #1: Creating the File 
  1. Create a text file (let's say unp.txt)
  2. Type the Username in first line and password in next line.
  3. save the file.











STEP #2: The Code!
  1. Create a class (let's say Mechanisms) with one method for getting the input from user for username and password (and) another method for comparing the user inputs with the username and password in the file.
  2. Here's the code for the Mechanisms class.

 class Mechanisms  
 {  
      char username[100];  
      char password[100];  
      char access_name[100];  
      char access_word[100];  
      int input;  
      int index;  
   
 public:  
   
      Mechanisms()  
      {  
           strcpy_s(username, "NoData");  
           strcpy_s(password, "NoData");  
           strcpy_s(access_name, "NoData");  
           strcpy_s(access_word, "Nodata");  
           input = 0;  
           index = 0;  
      }  
   
      void getInput(void)  
      {  
           printf("username : ");  
           scanf_s("%[^\n]", username, 99);  
   
           printf("password : ");  
   
           for (index = 0; (input = _getch()) != '\r';)  
           {  
                if ((input == '\b') && (index > 0))  
                {  
                     printf("\b");  
                     index--;  
                }  
                else if(input != '\b')  
                {  
                     printf("*");  
                     password[index] = (char)input;  
                     index++;  
                }  
           }  
           password[index] = '\0';  
      }  
   
      void compareInput(void)  
      {  
           FILE *file_pointer = NULL;  
           char wait = '0';  
   
           fopen_s(&file_pointer, "unp.txt", "r");  
           fscanf_s(file_pointer, "%s", access_name, 99);  
           fscanf_s(file_pointer, "%s", access_word, 99);  
   
           fclose(file_pointer);  
   
           if ((strcmp(access_name, username) == 0) && (strcmp(access_word, password) == 0))  
           {  
                printf("\n\n~login successful!~\n\n");  
           }  
           else  
           {  
                printf("\n\n~login not successful~\n\n");  
           }  
      }  
   
 };  


STEP #3: How to use it?
  1. In your main function, create an object for Mechanisms class.
  2. And call the two methods with the help of object that you've created.
  3. Here's the code :
 int main(void)  
 {  
      Mechanisms user;   
   
      user.getInput();  
      user.compareInput();  
   
     //NOTE: if wrong username and password are entered program will terminate  
       
     //write your original program here......  
   
      return 0;  
 }  

STEP #4: How to change Username and Password
  1. Open the text file containing your username and password and change them :)

I hope you understand :) any doubts let me know.... One interesting thing about this code is that when you enter password, * will appear instead of the characters you enter.

Some screenshots:




Comments

Popular posts from this blog

Beginner's guide to Solving the N-Queens problem using backtracking method

PvP Chain reaction game using Python

Guide to Solving Maximal Subarray Problem using Kadane's Algorithm