Search code examples
cfilevalidationpasswordsstrcmp

username and password validation turbo c


#include<stdio.h>
#include<conio.h>
#include<string.h>

int main()
{
clrscr();
char username[15], password[15], ch, usr[15], pass[15];
int choice, i=0, found=0;

    printf("1.Add Admin");
    printf("\n2.Log-in Admin");
    printf("\n3.Exit");
    printf("\nEnter your choice: ");
    scanf("%d",&choice);

    switch(choice)
    {
     case 1: FILE *acc;
        if((acc = fopen("c:\\account.txt","a+")) == NULL)
            printf("FILE NOT FOUND!");
        else
            {
            do
             {
              fscanf(acc,"%s %s%",username, password);
             }while(!feof(acc));
            }
        printf("\nEnter desired username: ");
        scanf("%s",username);
        printf("Enter desired password: ");

        while(ch != 13)
        {
         ch = getch();
         password[i] = ch;
         i++;
         printf("*");
        }
        password[i]='\0';
        fprintf(acc,"%s %s\n",username,password);
        fclose(acc);break;
     case 2: FILE *log;
        log = fopen("c:\\account.txt","r");

        printf("Username: ");   //user input username and password//
        scanf("%s",usr);
        printf("Password: ");

        while(ch != 13)
            {
            ch = getch();
            pass[i] = ch;
            i++;
            printf("*");
            }
            pass[i]='\0';
        while(!feof(log)&& found == 0)
        {
         fscanf(log,"%s %s",username,password);     //this is where i am having some problem//
         if(strcmp(usr,username) == 0 && strcmp(pass,password)); 
         found = 1;
         if(found == 1)
            printf("\nWelcome!");
         else
            printf("\nInvalid username or password");

        }

        fclose(log);
    }
getch();
return(0);
}

i can't get the validation working, it always says welcome even though it has wrong username and password. How to compare the user input string to the data inside the file? unfortunately turbo c is required.


Solution

  • The current password input is including the newline in the password, when the password is retrieved via fscanf, the newline is omitted.
    Try this password input for both password inputs

    printf("Enter desired password: ");
    i = 0;  // to be safe set i to zero                         
    while( ( ch = getch()) != '\n') // newline will be detected here and not added to password
    {                    
        password[i] = ch;
        i++;        
        printf("*");
        if ( i >= 14) { // to prevent a long password overwriting the array
            break;
        }
    }                                        
    password[i]='\0';
    

    EDIT:
    The '\n' worked fine for me on linux and gcc. With turbo c you may have to use instead of '\n' either 13, as you had to start, or maybe '\r'.
    If there are multiple sets of user/password in the file, you will see the welcome/invalid message for each set as that message is inside the while loop reading from the file.
    Try this to read from the file and validate the user/password

    found = 0;
    while( ( fscanf(log," %s %s",username,password == 2) {
    {             
        if(strcmp(usr,username) == 0 && strcmp(pass,password) == 0)
        {         
            found = 1;
            break;
        }         
    }             
    if(found == 1) // moved outside while loop so it is seen only once
        printf("\nWelcome!");
    else          
        printf("\nInvalid username or password");