CrazyEngineers
  • Hello Everyone!! i'm working on this C program which deals with user registration and login page. I was successful is writing the program for only 1 user. But now when i'm trying it for 100 users, i get a few problems that are:

    1. The program starts with registering 1st user records in w[0], it works fine if i login at this time.. But, when I close the program and restart it to make the second registration, again it starts with w[0] which copies the same username and password as the 1st registration.. How to overcome this problem???

    2. if I take just w instead of w[99] and make the necessary adjustments in the program, I'm able to get 2 different username's and passwords in the file but at the time of login, if program is able to read any of the username and password, it logs in..
    ex: suppose there are two username's and passwords (username followed by the password) as,
    abc pass abc1 pass1

    now, at the time of log in, if user enters username as abc and password as pass1, the program must not log in.. but it does.

    NEED SUGGESTIONS!!

    below, is the C code i've written.
    I'm facing problem in void reg() and void login()

    #include
    #include
    #include
    static int i=0;
    struct web
    {
    char name[30],pass[30];
    }w[99];
    int n;
    void login(void);
    void reg(void);
    int main()
    {
    clrscr();
    printf("
    
    
    
    
    				WELCOME TO MY WEBSITE");
    printf("
    				=====================");
    printf("
    
    
    
    			Press Enter to proceed...!!");
    if(getch()==13)
      clrscr();
    XY:
    printf("
    
    
    			1. LOGIN		2. REGISTER");
    printf("
    
    
    				ENTER YOUR CHOICE: ");
    scanf("%d",&n);
    switch(n)
      {
        case 1: clrscr();
            login();
            break;
        case 2: clrscr();
            reg();
            break;
        default: printf("
    
    				NO MATCH FOUND");
            printf("
    
    			Press Enter to re-Enter the choice");
            if(getch()==13)
            clrscr();
            goto XY;
      }
      return 0;
    }
    void reg()
      {
        FILE *fp;
        char c,checker[30]; int z=0;
        fp=fopen("Web_reg.txt","ab+");
        printf("
    
    				WELCOME TO REGISTER ZONE");
        printf("
    				^^^^^^^^^^^^^^^^^^^^^^^^");
        for(i=0;i<100;i++)
        {
          printf("
    
    				  ENTER USERNAME: ");
          scanf("%s",checker);
            while(!feof(fp))
            {
              fread(&w[i],sizeof(w[i]),1,fp);
              if(strcmp(checker,w[i].name)==0)
                {
                printf("
    
    			USERNAME ALREDY EXISTS");
                clrscr();
                reg();
                }
              else
              {
                strcpy(w[i].name,checker);
                break;
              }
            }
          printf("
    
    				  DESIRED PASSWORD: ");
          while((c=getch())!=13)
            {
              w[i].pass[z++]=c;
              printf("%c",'*');
            }
          fwrite(&w[i],sizeof(w[i]),1,fp);
          fclose(fp);
          printf("
    
    	Press enter if you agree with Username and Password");
          if((c=getch())==13)
            {
            clrscr();
            printf("
    
    		You are successfully registered");
            printf("
    
    		Try login your account??
    
    		  ");
            printf("> PRESS 1 FOR YES
    
    		  > PRESS 2 FOR NO
    
    				");
            scanf("%d",&n);
            switch(n)
              {
                  case 1: clrscr();
                        login();
                        break;
                  case 2: clrscr();
                        printf("
    
    
    					THANK YOU FOR REGISTERING");
                        break;
              }
            }
            break;
          }
        getch();
      }
      void login()
        {
          FILE *fp;
          char c,name[30],pass[30]; int z=0;
          int checku,checkp;
          fp=fopen("Web_reg.txt","rb");
          printf("
    
    				WELCOME TO LOG IN ZONE");
          printf("
    				^^^^^^^^^^^^^^^^^^^^^^");
          for(i=0;i<1000;i++)
          {
            printf("
    
    				  ENTER USERNAME: ");
            scanf("%s",name);
            printf("
    
    				  ENTER PASSWORD: ");
            while((c=getch())!=13)
            {
              pass[z++]=c;
              printf("%c",'*');
            }
            pass[z]='';
          while(!feof(fp))
            {
            fread(&w[i],sizeof(w[i]),1,fp);
              checku=strcmp(name,w[i].name);
              checkp=strcmp(pass,w[i].pass);
              if(checku==0&&checkp==0)
              {
                clrscr();
                printf("
    
    
    			YOU HAVE LOGGED IN SUCCESSFULLY!!");
                printf("
    
    
    				WELCOME, HAVE A NICE DAY");
                break;
              }
            else if(checku==0&&checkp!=0)
              {
                printf("
    
    
    			WRONG PASSWORD!! Not %s??",name);
                printf("
    
    				  (Press 'Y' to re-login)");
                if(getch()=='y'||getch()=='Y')
                  login();
              }
            else if(checku!=0)
              {
                printf("
    
    
    			You are not a Registered User
     			Press enter to register yourself");
                if(getch()==13)
                clrscr();
                reg();
              }
            }
            break;
          }
          getch();
        }

    Update:

    Following is the updated User login and registration program in C:

    #include 
    #include 
    
    #define MAX_USERS 100
    #define MAX_USERNAME_LENGTH 20
    #define MAX_PASSWORD_LENGTH 20
    
    struct User {
        char username[MAX_USERNAME_LENGTH];
        char password[MAX_PASSWORD_LENGTH];
    };
    
    struct User users[MAX_USERS];
    int numUsers = 0;
    
    void registerUser() {
        if (numUsers == MAX_USERS) {
            printf("Maximum number of users reached.\n");
            return;
        }
    
        struct User newUser;
    
        printf("Enter username: ");
        scanf("%s", newUser.username);
    
        printf("Enter password: ");
        scanf("%s", newUser.password);
    
        users[numUsers] = newUser;
        numUsers++;
    
        printf("Registration successful.\n");
    }
    
    int loginUser() {
        char username[MAX_USERNAME_LENGTH];
        char password[MAX_PASSWORD_LENGTH];
    
        printf("Enter username: ");
        scanf("%s", username);
    
        printf("Enter password: ");
        scanf("%s", password);
    
        for (int i = 0; i < numUsers; i++) {
            if (strcmp(username, users[i].username) == 0 && strcmp(password, users[i].password) == 0) {
                printf("Login successful.\n");
                return 1;
            }
        }
    
        printf("Invalid username or password.\n");
        return 0;
    }
    
    int main() {
        int choice;
    
        do {
            printf("1. Register\n");
            printf("2. Login\n");
            printf("3. Exit\n");
            printf("Enter your choice: ");
            scanf("%d", &choice);
    
            switch (choice) {
                case 1:
                    registerUser();
                    break;
                case 2:
                    if (loginUser()) {
                        // Perform actions for logged-in user
                    }
                    break;
                case 3:
                    printf("Exiting...\n");
                    break;
                default:
                    printf("Invalid choice. Please try again.\n");
                    break;
            }
        } while (choice != 3);
    
        return 0;
    }

    Explaination:

    In this program, we use a struct called User to represent a user with a username and password. We maintain an array users to store registered users, with a maximum capacity defined by MAX_USERS. The variable numUsers keeps track of the current number of registered users.

    The registerUser() function prompts the user to enter a username and password, stores them in a new User struct, and adds it to the users array.

    The loginUser() function prompts the user to enter a username and password and checks if they match any of the registered users. If a match is found, it prints a success message; otherwise, it prints an error message.

    The main() function presents a menu with options for registration, login, and exit. It repeatedly asks the user for a choice and performs the corresponding action until the user chooses to exit.

    Note that this is a simplified example for learning purposes and not meant for production use. It does not include error handling, input validation, or secure password storage.

    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Kaustubh Katdare

    AdministratorJul 12, 2012

    We've an unanswered thread, folks! Where are all our computing experts? 😀
    Are you sure? This action cannot be undone.
    Cancel
  • Uday Bidkar

    MemberJul 14, 2012

    I don't have a C compiler with me, but do see some potential errors in code.

    0) The way you check for "user name exist" is faulty. You are not going through entire file to search the username. The first failure to match the user name breaks the loop.
    1) I can see you are using recursion in case user name already exists. Close the file pointer before the call to reg() and call "return" just after you call reg(). This will ensure that the calling reg() wont continue below that point when the called reg() returns.
    Recursion is extremely great tool and at the same time very dangerous if not used right 😀
    2) You don't initialize the value of z to zero before setting the password for a new user

    Try fixing these first and we'll take take it from there later.
    Are you sure? This action cannot be undone.
    Cancel
  • Vishal Sharma

    MemberJul 14, 2012

    Uday Bidkar
    I don't have a C compiler with me, but do see some potential errors in code.

    0) The way you check for "user name exist" is faulty. You are not going through entire file to search the username. The first failure to match the user name breaks the loop.
    1) I can see you are using recursion in case user name already exists. Close the file pointer before the call to reg() and call "return" just after you call reg(). This will ensure that the calling reg() wont continue below that point when the called reg() returns.
    Recursion is extremely great tool and at the same time very dangerous if not used right 😀
    2) You don't initialize the value of z to zero before setting the password for a new user

    Try fixing these first and we'll take take it from there later.

    Thanks for your reply!! but i've fixed this error!! 😉
    Are you sure? This action cannot be undone.
    Cancel
  • ibrar

    MemberOct 28, 2013

    Can you please share you code. I have some requirement in my program and it will be highly appreciated if you can help here :


    1.Login Screen

    When use enters user name and password in the login screen should be matched against

    and encrypted flat file and allowed access if match is successful for userid

    Not of invalid attempts should be logged and id disabled when more than consecutive 5 attempts
    Are you sure? This action cannot be undone.
    Cancel
  • ibrar

    MemberOct 28, 2013

    Add/delete users
    i.Supervisor id should allowed to update new users to encrypted password file GUI
    ii.Supervisor id should be allowed to delete new users from encrypted password file entered with GUI
    Are you sure? This action cannot be undone.
    Cancel
  • Vishal Sharma

    MemberOct 28, 2013

    This is a very old thread!
    Problem has been rectified long back!
    Are you sure? This action cannot be undone.
    Cancel
  • denish aarons

    MemberApr 12, 2014

    Vishal0203 how did u fix the program, really need to know, please get back to me
    Are you sure? This action cannot be undone.
    Cancel
  • Joshua Dani M

    MemberOct 17, 2016

    vishal0203 could you please send the final code...it would be really helpful
    Are you sure? This action cannot be undone.
    Cancel
  • Joshua Dani M

    MemberOct 17, 2016

    Vishal0203
    This is a very old thread!
    Problem has been rectified long back!
    could you send the rectified code...it would help in my mini project
    Are you sure? This action cannot be undone.
    Cancel
  • Joshua Dani M

    MemberOct 19, 2016

    #-Link-Snipped-# please send the complete code!
    Are you sure? This action cannot be undone.
    Cancel
  • Vishal Sharma

    MemberOct 19, 2016

    Joshua Dani M
    #-Link-Snipped-# please send the complete code!
    Hey, I'm afraid i do not have this code with me anymore, as you see this is a 4-5 year old thread. However, you can use struct to solve this.

    Open the file in binary mode, write the struct with username and password to the file. You can then load the data of the file as initialization and store it in array of structs. Then take the input of username and password and check your array for the match.

    Note : this solution is not optimal, but my requirements weren't that great.
    Are you sure? This action cannot be undone.
    Cancel
  • Joshua Dani M

    MemberOct 19, 2016

    Vishal0203
    Hey, I'm afraid i do not have this code with me anymore, as you see this is a 4-5 year old thread. However, you can use struct to solve this.

    Open the file in binary mode, write the struct with username and password to the file. You can then load the data of the file as initialization and store it in array of structs. Then take the input of username and password and check your array for the match.

    Note : this solution is not optimal, but my requirements weren't that great.
    Thank you! But i'm not that great in programming! could you modify the above program...it will be very helpful to me...and ill be grateful to you
    Are you sure? This action cannot be undone.
    Cancel
  • Vishal Sharma

    MemberOct 19, 2016

    Joshua Dani M
    Thank you! But i'm not that great in programming! could you modify the above program...it will be very helpful to me...and ill be grateful to you
    No assignment helps.
    Are you sure? This action cannot be undone.
    Cancel
  • Joshua Dani M

    MemberOct 19, 2016

    Vishal0203
    No assignment helps.
    Thanks anyways! Btw great code and idea!
    Are you sure? This action cannot be undone.
    Cancel
  • Joshua Dani M

    MemberOct 19, 2016

    Joshua Dani M
    Thank you! But i'm not that great in programming! could you modify the above program...it will be very helpful to me...and ill be grateful to you
    But isnt that what you did in the code #-Link-Snipped-# ? Or am i mistaken?
    Are you sure? This action cannot be undone.
    Cancel
  • Vishal Sharma

    MemberOct 22, 2016

    Joshua Dani M
    But isnt that what you did in the code #-Link-Snipped-# ? Or am i mistaken?
    Yeah I did some modifications in the same code to get it work.
    Just not interested to study C code anymore 😀
    Are you sure? This action cannot be undone.
    Cancel
  • eagha

    MemberApr 7, 2018

    pls send crt program bro
    Are you sure? This action cannot be undone.
    Cancel
  • alberth ruado

    MemberOct 27, 2018

    #include&lt;conio.h&gt;

    #include&lt;stdio.h&gt;

    #include&lt;string.h&gt;

    static int i=0;

    struct web

    {

    char name[30],pass[30];

    }w[99];

    int n;

    void login(void);

    void reg(void);

    int main()

    {

    clrscr();

    printf("\n\n\n\n\n\t\t\t\tWELCOME TO MY WEBSITE");

    printf("\n\t\t\t\t=====================");

    printf("\n\n\n\n\t\t\tPress Enter to proceed...!!");

    if(getch()==13)

      clrscr();

    XY:

    printf("\n\n\n\t\t\t1. LOGIN\t\t2. REGISTER");

    printf("\n\n\n\t\t\t\tENTER YOUR CHOICE: ");

    scanf("%d",&amp;n);

    switch(n)

      {

        case 1: clrscr();

            login();

            break;

        case 2: clrscr();

            reg();

            break;

        default: printf("\n\n\t\t\t\tNO MATCH FOUND");

            printf("\n\n\t\t\tPress Enter to re-Enter the choice");

            if(getch()==13)

            clrscr();

            goto XY;

      }

      return 0;

    }

    void reg()

      {

        FILE *fp;

        char c,checker[30]; int z=0;

        fp=fopen("Web_reg.txt","ab+");

        printf("\n\n\t\t\t\tWELCOME TO REGISTER ZONE");

        printf("\n\t\t\t\t^^^^^^^^^^^^^^^^^^^^^^^^");

        for(i=0;i&lt;100;i++)

        {

          printf("\n\n\t\t\t\t  ENTER USERNAME: ");

          scanf("%s",checker);

            while(!feof(fp))

            {

              fread(&amp;w[i],sizeof(w[i]),1,fp);

              if(strcmp(checker,w[i].name)==0)

                {

                printf("\n\n\t\t\tUSERNAME ALREDY EXISTS");

                clrscr();

                reg();

                }

              else

              {

                strcpy(w[i].name,checker);

                break;

              }

            }

          printf("\n\n\t\t\t\t  DESIRED PASSWORD: ");

          while((c=getch())!=13)

            {

              w[i].pass[z++]=c;

              printf("%c",'*');

            }

          fwrite(&amp;w[i],sizeof(w[i]),1,fp);

          fclose(fp);

          printf("\n\n\tPress enter if you agree with Username and Password");

          if((c=getch())==13)

            {

            clrscr();

            printf("\n\n\t\tYou are successfully registered");

            printf("\n\n\t\tTry login your account??\n\n\t\t  ");

            printf("&gt; PRESS 1 FOR YES\n\n\t\t  &gt; PRESS 2 FOR NO\n\n\t\t\t\t");

            scanf("%d",&amp;n);

            switch(n)

              {

                  case 1: clrscr();

                        login();

                        break;

                  case 2: clrscr();

                        printf("\n\n\n\t\t\t\t\tTHANK YOU FOR REGISTERING");

                        break;

              }

            }

            break;

          }

        getch();

      }

      void login()

        {

          FILE *fp;

          char c,name[30],pass[30]; int z=0;

          int checku,checkp;

          fp=fopen("Web_reg.txt","rb");

          printf("\n\n\t\t\t\tWELCOME TO LOG IN ZONE");

          printf("\n\t\t\t\t^^^^^^^^^^^^^^^^^^^^^^");

          for(i=0;i&lt;1000;i++)

          {

            printf("\n\n\t\t\t\t  ENTER USERNAME: ");

            scanf("%s",name);

            printf("\n\n\t\t\t\t  ENTER PASSWORD: ");

            while((c=getch())!=13)

            {

              pass[z++]=c;

              printf("%c",'*');

            }

            pass[z]='\0';

          while(!feof(fp))

            {

            fread(&amp;w[i],sizeof(w[i]),1,fp);

              checku=strcmp(name,w[i].name);

              checkp=strcmp(pass,w[i].pass);

              if(checku==0&amp;&amp;checkp==0)

              {

                clrscr();

                printf("\n\n\n\t\t\tYOU HAVE LOGGED IN SUCCESSFULLY!!");

                printf("\n\n\n\t\t\t\tWELCOME, HAVE A NICE DAY");

                break;

              }

            else if(checku==0&amp;&amp;checkp!=0)

              {

                printf("\n\n\n\t\t\tWRONG PASSWORD!! Not %s??",name);

                printf("\n\n\t\t\t\t  (Press 'Y' to re-login)");

                if(getch()=='y'||getch()=='Y')

                  login();

              }

            else if(checku!=0)

              {

                printf("\n\n\n\t\t\tYou are not a Registered User\n \t\t\tPress enter to register yourself");

                if(getch()==13)

                clrscr();

                reg();

              }

            }

            break;

          }

          getch();

        }

    Are you sure? This action cannot be undone.
    Cancel
  • Kaustubh Katdare

    AdministratorOct 27, 2018

    Posting on behalf of #-Link-Snipped-# 

    Registration process:

    the system should check the admin is a existing user,if he is not then the system should check the admin is a student or a faculty.If he is a student then the system should take all the required details for the registration process and make a username and password for the new user to login the college website in future.the same is required for the faculty too.

    Are you sure? This action cannot be undone.
    Cancel
  • Deepika Tankasala

    MemberDec 29, 2018

    I have tried to execute the code but there is a problem.Once a person has registered and tries to login the system still says "you aren't a registered user" could you help in this regard.Please reply at the earliest!

    Are you sure? This action cannot be undone.
    Cancel
  • Rakibul Pranto

    MemberFeb 25, 2019

    I have tried to execute the code but there is a problem.twice a person has registered and tries to login the system it says "you aren't a registered user" could you help in this regard.Please reply at the earliest! Tomorrow i have to submit my project to my teacher

    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register