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(); }
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.
Uday BidkarI 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!! 😉
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
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
Problem has been rectified long back!
could you send the rectified code...it would help in my mini projectVishal0203This is a very old thread!
Problem has been rectified long back!
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.Joshua Dani M@Vishal0203 please send the complete code!
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 youVishal0203Hey, 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.
No assignment helps.Joshua Dani MThank 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
Thanks anyways! Btw great code and idea!Vishal0203No assignment helps.
But isnt that what you did in the code @Vishal0203 ? Or am i mistaken?Joshua Dani MThank 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
Yeah I did some modifications in the same code to get it work.Joshua Dani MBut isnt that what you did in the code @Vishal0203 ? Or am i mistaken?
Just not interested to study C code anymore 😀
#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();
}
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.
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!
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
Rakibul Pranto,How did you solve this problem? I'd really appreciate if you reply ASAP.
- #include <stdio.h>
- #include <string.h>
- #include <conio.h>
- int main()
- {
- char username[15];
- char password[12];
- printf("Enter your username:\n");
- scanf("%s",&username);
- printf("Enter your password:\n");
- scanf("%s",&password);
- if(strcmp(username,"chai")==0){
- if(strcmp(password,"123")==0){
- printf("\nWelcome.Login Success!");
- }else{
- printf("\nwrong password");
- }
- }else{
- printf("\nUser doesn't exist");
- }
- return 0;
- }
/* 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");
}
Related Posts
@Ankita Katdare · Jun 9, 2011
@aieeeshravan · Jun 30, 2009
@Ankita Katdare · Aug 18, 2013
@Kaustubh Katdare · Oct 29, 2013
@mujju433 · Oct 1, 2008