CrazyEngineers Archive
Old, but evergreen and popular discussions on CrazyEngineers, presented to you in read-only mode.
@Vishal Sharma • 17 Jun, 2012 • 20 likes
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<conio.h>
#include<stdio.h>
#include<string.h>
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",&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<100;i++)
    {
      printf("\n\n\t\t\t\t  ENTER USERNAME: ");
      scanf("%s",checker);
        while(!feof(fp))
        {
          fread(&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(&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("> PRESS 1 FOR YES\n\n\t\t  > PRESS 2 FOR NO\n\n\t\t\t\t");
        scanf("%d",&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<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(&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("\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&&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();
    }
@Kaustubh Katdare • 12 Jul, 2012 We've an unanswered thread, folks! Where are all our computing experts? 😀
@Uday Bidkar • 14 Jul, 2012 • 1 like 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.
@Vishal Sharma • 14 Jul, 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!! 😉
@ibrar • 28 Oct, 2013 • 1 like 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
@ibrar • 28 Oct, 2013 • 1 like 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
@Vishal Sharma • 28 Oct, 2013 • 2 likes This is a very old thread!
Problem has been rectified long back!
@denish aarons • 12 Apr, 2014 Vishal0203 how did u fix the program, really need to know, please get back to me
@Joshua Dani M • 17 Oct, 2016 vishal0203 could you please send the final code...it would be really helpful
@Joshua Dani M • 17 Oct, 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
@Joshua Dani M • 19 Oct, 2016 @Vishal0203 please send the complete code!
@Vishal Sharma • 19 Oct, 2016
Joshua Dani M
@Vishal0203 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.
@Joshua Dani M • 19 Oct, 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
@Vishal Sharma • 19 Oct, 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.
@Joshua Dani M • 19 Oct, 2016
Vishal0203
No assignment helps.
Thanks anyways! Btw great code and idea!
@Joshua Dani M • 19 Oct, 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 @Vishal0203 ? Or am i mistaken?
@Vishal Sharma • 23 Oct, 2016 • 1 like
Joshua Dani M
But isnt that what you did in the code @Vishal0203 ? 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 😀
@eagha • 07 Apr, 2018 pls send crt program bro
@alberth ruado • 27 Oct, 2018 • 1 like

#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();

    }

@Kaustubh Katdare • 27 Oct, 2018

Posting on behalf of @tulluru 

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.

@Deepika Tankasala • 29 Dec, 2018 • 2 likes

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!

@Rakibul Pranto • 25 Feb, 2019 • 3 likes

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

@Hassan shaikh • 29 Nov, 2019 • 2 likes

Rakibul Pranto,How did you solve this problem? I'd really appreciate if you reply ASAP.

@Divisha Madupalli • 20 Feb, 2020
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <conio.h>
  4.  
  5. int main()
  6. {
  7.     char username[15];
  8.     char password[12];
  9.  
  10.  
  11.     printf("Enter your username:\n");
  12.     scanf("%s",&username);
  13.  
  14.     printf("Enter your password:\n");
  15.     scanf("%s",&password);
  16.  
  17.     if(strcmp(username,"chai")==0){
  18.         if(strcmp(password,"123")==0){
  19.  
  20.         printf("\nWelcome.Login Success!");
  21.  
  22.  
  23.         }else{
  24.     printf("\nwrong password");
  25. }
  26.     }else{
  27.     printf("\nUser doesn't exist");
  28. }
  29.     return 0;
  30. }

@Teja Reddy • 25 Feb, 2020

/* login.c
Author: Jake Rodriguez Pomperada,MAED-IT
Website: http://www.jakerpomperada.com
Emails: jakerpomperada@gmail.com and jakerpomperada@aol.com
Location : Bacolod City, Negros Occidental
Tool : Dev C++ Version 5.11
Date : January 7, 20193:46 PM Monday
*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
void get_ID_and_PASS(char fileName[30],char *id,char *pass)
{
FILE *F = fopen(fileName,"r");
if(F)
{
int count = 0;
while(!feof(F))
{
char rawLine[50];
fscanf(F,"%s",rawLine);
if(!count++)
strcpy(id,rawLine);
else
strcpy(pass,rawLine);
}
}else printf("Cannot open this file");
fclose(F);
}
int main()
{
char fileName[30] = "users.txt";
char userID[50],userPassW[50];
char strID[50]="\0",strPASSW[50]="\0";
char IDpref[50] = "user_id:\0",PASSWpref[50] = "password:\0";
get_ID_and_PASS(fileName,userID,userPassW);
char c;
int pos = 0;
printf("\n\n");
printf("\tLOGIN SECURITY SYSTEM IN C USING TEXT FILES");
printf("\n\n");
printf("\tEnter User Name : ");
scanf("%s",&strID);
printf("\n");
printf("\tEnter Your Password : ");
do {
c = getch();
if( isprint(c) )
{
strPASSW[ pos++ ] = c;
printf("%c", '*');
}
else if( c == 9 && pos )
{
strPASSW[pos--] = '\0';
printf("%s", "\b \b");
}
} while( c != 13 );
strcpy(strID,strcat(IDpref,strID));
strcpy(strPASSW,strcat(PASSWpref,strPASSW));
if (!strcmp(strID,userID)&&!strcmp(strPASSW,userPassW))
{
printf("\n\n");
printf("\tCorrect Username And Password\n");
printf("\n\n\tWelcome to the System\n\n");
}
else
{
printf("\n\n");
printf("\tInvalid Username And Password. Try Again\n\n");
}
printf("\n\n");
printf("\tEND OF PROGRAM");
printf("\n\n");
system("pause");
}

32.8k views

Related Posts

@Ankita Katdare · Jun 9, 2011

It's official. After the release of B.E. 6th Semester results, now it's turn of B.E. 4th Sem results. They will be out in the wee hours of night i.e. at...
4.2k views

@aieeeshravan · Jun 30, 2009

wat r the softwares used by electronics and communication students
7.8k views

@Ankita Katdare · Aug 18, 2013

FIAT Group Automobiles India Private Limited has launched the new Fiat Punto Sport edition as the 2013 model of the car at the FIAT Caffe in Bengaluru. Though the changes...
4.8k views

@Kaustubh Katdare · Oct 29, 2013

Reading about India's upcoming ambitious Mars mission, I discovered that ISRO and Indian Government is spending over Rs. 450 crore to send a satellite to orbit mars. What's more shocking...
3.6k views

@mujju433 · Oct 1, 2008

I heard that servomotor uses an encoder? Whats the function of encoder in servomotor?? I am working on one of the machine which is used for tabbering and stringing of...
3.2k views