Code:
do
{
password[x]=getch();
if(password[x]=='\r') /*Check for return key*/
break;
else if(password[x]=='\b') /*Check for backspace key*/
{
if(x==0)
{
continue;
}
else
{
putchar('\b');
putchar(' ');
x--;
}
}
else /*User still keying in password*/
{
x++;
}
}while(password[x]!='\r');
password[x]='\0';
The above code hides the characters the user is typing when keying his/her password. I found a problem. When I use this code, if I type in the wrong password for the first attempt, I willl still get ACCESS DENIED even if I key in the correct ID and password in the second attempt. However, if I simply use the scanf("%s", %password) function which doesn't hide the password characters, the problem won't appear. The following code is the function to check for valid ID and password.
Code:
int CheckPass(char *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(ID))
{
for(i=0, temp=strlen(fuser_id); i<temp; i++)
{
found=1;
if(*(ID+i)!=fuser_id[i])
{
fclose(fpPass);
found=0;
}
}
if(found==0)
{
fclose(fpPass);
return 0;
}
else
{
/*Check for valid password*/
if(strlen(password)!=strlen(fpassword))
{
fclose(fpPass);
return 0;
}
else
{
for(i=0, temp=strlen(fpassword); i<temp; i++)
if(*(password+i)!=fpassword[i]-2)
{
fclose(fpPass);
return 0;
}
else
{
fclose(fpPass);
return 1;
}
}
}
}
}
return 0;
}
Please help!!!