Reputation: 137
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 :)
Upvotes: 1
Views: 289
Reputation: 21
The condition you are looking for is when the program has checked all the names in the file (reached the end-of-file) but hasn't found any valid ones. You're going to need a variable to keep track of whether any valid name has been found. Whenever you get to the section of code handling the two names being equal, you set the variable to 1. Otherwise it remains its default value. This way after you exit the loop (reach the end of the file) and the variable is not changed, that means none of the names in the file were the same as the one entered by the user. The other thing you want - giving the user the ability to try again - is also simple. You want to repeat the whole process of asking for a name and checking whether it's correct or not until the user is identified. The way to do that would be using another loop. So, something like this:
#include <errno.h>
#include <stdio.h>
#include <string.h>
const char* list_file_name = "admin_users.txt";
int main(int argc, char** argv) {
FILE* list_file;
char loginID[200], name[200];
int identified = 0, error = 0;
if ((list_file = fopen(list_file_name, "r")) == NULL) {
error = errno;
fprintf(stderr, "Couldn't open users.\n");
return error;
}
while (identified == 0) {
printf("Please enter your login ID: ");
if (!fgets(loginID, 200, stdin)) {
error = errno;
fprintf(stderr, "\nCan't read login ID.");
goto cleanup;
}
while (fgets(name, 200, list_file)) {
if (!strcmp(name, loginID)) {
identified = 1;
printf("Welcome %s", name);
break;
}
}
if (identified == 0)
printf("Incorrect username. Please try again.\n");
rewind(list_file);
}
cleanup:
fclose(list_file);
return error;
}
What you should consider with these problems is when do I want to print the error and once you know that, you should find the place in your code that matches the point you are looking for. If there's no block like that anywhere, you will have to create it by adding more conditions.
Upvotes: 2
Reputation: 111
I change your code, so that the user is asked for a login ID until a valid ID was found or the user inputs an empty string. If a valid login ID was found the loop does not continue. See the code above and read the comments:
FILE *fp =
fopen("C:\\Users\\khali\\Desktop\\C programming project\\admin.txt", "r");
char loginID [200];
char password [200];
char name[200];
int found = 0; // will become 1 if the login ID was found
while (fp && !found) {
printf("Please enter your login ID below\n");
scanf("%s", &loginID);
if ( loginID[0] == 0 ) { // test if the inut is an empty string and break
break;
}
fseek(fp, 0, SEEK_SET); // start every search at the beginning of the file
while (!found && !feof(fp)) { // test if found or end of file
fgets(name, 200, fp );
if (strncmp(name, loginID, strlen(loginID)) == 0) {
found = 1; // the name was found and we are finished
printf("\nWelcome %s", name);
}
}
}
if (fp) {
fclose(fp);
}
Upvotes: 2