#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int CheckPass(char *user_id, char *password);
void AccessGranted();
void main()
{
char user_id[20], password[20];
int flag, change=3;
int count=3;
do
{
/*Show the number of attempt(s) left*/
printf("Number of attempt(s) left: %d\n", count);
/*Gets user ID from user*/
fflush(stdin);
printf("Enter your user ID > ");
scanf("%[^\n]", user_id);
/*Gets password from user*/
fflush(stdin);
printf("Enter your password > ");
scanf("%[^\n]", password);
system("cls");
flag=CheckPass(user_id, password);
if(flag==1)
{
printf("\nACCESS GRANTED\n\n");
AccessGranted();
}
else
{
printf("\nACCESS DENIED\nUser ID and password does not match\n\n");
count--;
if(count==0)
printf("\nYou have no more attempt(s) left.\nProgram terminated\n\n");
}
}while(count>0);
}
int CheckPass(char *user_id, char *password)
{
FILE *fpPass;
char fuser_id[20], fpassword[20];
int i, temp;
int found=0;
if(!(fpPass=fopen("Password.txt", "r")))
{
printf("Error: Unable to open password file.\nSorry for the inconvenience\n\n");
exit(0);
}
while(fscanf(fpPass, "%[^\n]\n%[^\n]\n", fuser_id, fpassword)!=EOF && !found)
{
/*Check for valid user ID*/
if(strlen(fuser_id)==strlen(user_id))
for(i=0, temp=strlen(fuser_id); i<temp; i++)
{
found=1;
if(*(user_id+i)!=fuser_id[i])
found=0;
}
}
fclose(fpPass);
if(!found)
return 2;
else
{
/*Check for valid password*/
if(strlen(password)!=strlen(fpassword))
return 0;
else
{
for(i=0, temp=strlen(fpassword); i<temp; i++)
if(*(password+i)!=fpassword[i]-2)
return 0;
}
}
return 1;
}
void AccessGranted()
{
printf("Thank you for using this system.\n\n");
exit(0);
}
The above program read input from user (ID and password).
The encrypted password is stored in Password.txt. The contain of Password.txt:
joe
dcvocp
joe1
dcvocp3
The problem is, when user key in joe, the correct password is batman1, not batman. Please help!!!