-2
#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.

Dme12
  • 3
  • 1
  • 4

2 Answers2

0

in your code, when you are comparing the username and password

if(strcmp(usr,username) == 0 && strcmp(pass,password));

at the strcmp of password, the check whether it has returned 0 or not is missing. Try this

if(strcmp(usr,username) == 0 && strcmp(pass,password) == 0)

And, your if loop has a ; in the end, that means the found = 1 is not inside the if loop, remove the ;

Haris
  • 12,120
  • 6
  • 43
  • 70
0

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");
user3121023
  • 8,181
  • 5
  • 18
  • 16
  • it kinda works, however what if i would like to stop by pressing enter during password input? it also displays the invalid message 2 times if invalid usrname and password. – Dme12 Oct 09 '14 at 13:40