So I managed to code a solution or finding names in files to validate login. If the ID is there in the file, the program will allow the user to go further. But the issue is, if the user doesn't enter a registered name, the code will just crash, as there is no way to put an error message. Here's the code:
void loginadmin()
{
FILE *fp = fopen("C:\\Users\\khali\\Desktop\\C programming project\\admin.txt", "r");
char loginID [200];
char password [200];
char name[200];
printf("Please enter your login ID below\n");
scanf("%s", &loginID);
while (!feof(fp))
{
fgets(name, 200, fp );
if (strncmp(name, loginID, strlen(loginID)) == 0)
{
printf("\nWelcome %s", name);
}
}
fclose(fp);
}
Now in this code, the part:
while (!feof(fp))
{
fgets(name, 200, fp );
if (strncmp(name, loginID, strlen(loginID)) == 0)
{
printf("\nWelcome %s", name);
}
}
fclose(fp);
}
I wrote it in such a way that it read every line in file, showed validity, and then went on the next line:
while (!feof(fp))
{
fgets(name, 200, fp );
if (strncmp(name, loginID, strlen(loginID)) == 0)
{
printf("\nWelcome %s", name);
}
else;
{
printf("\nWrong input");
}
}
fclose(fp);
}
Can someone help me put the validation in correct way so that the code only shows validation line once and doesnt run the code again and again and print validity of the code again and again until the correct name has been reached, and also to give users another try instead of running the code all over again. Cheers :)