In the program i made tried to login and it does not work i am not sure what i did wrong.
[Click Here](http://www.onlinegdb.com/B15uh6kB_)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXUSERNAME 30
#define MAXPASSWORD  20

typedef struct
{
    char username[MAXUSERNAME];
    char password[MAXPASSWORD];
} pass;

int main()
{
    char userName[MAXUSERNAME];
    char password[MAXPASSWORD];
    pass t;
    int c;
    FILE *P= fopen("teachers.txt", "rb");

    if (P == NULL) 
    {
        printf("Error! opening file");
        exit(1);
    }

    do
    {
        printf("\nUsername:");
        fgets(userName,MAXUSERNAME,stdin);
        printf("\nPassword:");
        fgets(password,MAXPASSWORD,stdin);
        if((!strcmp(userName, t.username)) && (!strcmp(password, t.password)))
        {
           printf("Welcome to the Ahjin Private Primary School Repot Renerating System");
        }
        else
        {
            printf("\nLogin Failed \nEnter Username & Password Again\n");
            c++;
        }
    }
    while(c<=3);
    if(c>3)
    {
        printf("Login Failed");
        printf("Sorry,Unknown User.");
    }
}

Recommended Answers

All 2 Replies

The code as is works fine. Since there is no code to read the file and look for a match, all logins fail.

I see you tagged this as homework. Remember that you get to design and write the missing code.

Also, given that passwords shall NEVER be stored in plain text in a file, this would get a failing grade from me.

More at https://blog.moertel.com/posts/2006-12-15-never-store-passwords-in-a-database.html

What is the error...?
Your code looks fine

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.