Log in form is not working it crashed after I write the username and password even though my password is correct in the file. I think the problem is the condition of the login form.
#include <stdio.h> #include <stdlib.h> #include <string.h> struct employeeInfo{ char fullName[100]; char bDay[100]; char age[3]; char position[100]; char address[100]; char sex[100]; char cpNum[50]; char email[100]; char userName[100]; char passWord[100]; char salary[50]; char attendance[50]; char loan[50]; char events[100]; char requestLetter[100]; char idNumber[10]; }; //FILE FILE *fptr_admin; FILE *fptr_employee; //GLOBAL VARIBALESS struct employeeInfo Employee; //FUNCTIONS //EMPLOYEE void log_in_employee(); void employee_page(); //ADMIN void add_employee();
main fuction
int main(){ char choice; printf("1. Log in as Employee\n"); printf("2. Log In as Admin\n"); printf("3. Exit\n"); printf("Enter your choice: "); scanf(" %c", &choice); switch(choice){ case'1': log_in_employee(); break; case '2': break; case '3': break; default: printf("Wrong input!"); break; } return 0; }
adding employee
void add_employee(){ char choice, inputOne, inputTwo, sex; fptr_employee = fopen("employee.bin", "ab"); fptr_admin = fopen("admin.bin", "ab"); if((fptr_employee == NULL) || (fptr_admin == NULL)){ printf("File not found."); exit(1); } while(1){ printf("\nPersonal Information\n\n"); printf("Enter your full name (SN, FN, MN): "); fflush(stdin); gets(Employee.fullName); printf("Enter Id Number: "); gets(Employee.idNumber); printf("Enter your Birthday (M/D/Y): "); gets(Employee.bDay); printf("Enter your age: "); scanf("%[0-9]", &Employee.age); printf("Enter your address: "); fflush(stdin); gets(Employee.address); printf("\n1. Male\n"); printf("2. Female\n"); printf("3. Others\n"); printf("Enter your sex: "); select: scanf(" %c", &inputOne); switch(inputOne){ case '1': strcpy(Employee.sex, "Male"); break; case '2': strcpy(Employee.sex, "Female"); break; case '3': printf("Please specify here: "); scanf("%s", Employee.sex); break; default: goto select; break; } printf("\n"); printf("Enter your cellphone number: "); fflush(stdin); scanf("%[0-9]", &Employee.cpNum); printf("Enter your email address: "); fflush(stdin); gets(Employee.email); printf("\nLog In Details\n\n"); printf("Enter your user name with this format(empl + first name + last name): "); scanf(" %s",Employee.userName); printf("Enter your password: "); scanf(" %s", Employee.passWord); printf("\n1. Regular Employee\n"); printf("2. Admin\n"); printf("Enter your position in company: "); position: scanf(" %c", &inputTwo); //choice position switch(inputTwo){ case '1': strcpy(Employee.position, "Regular Employee"); fwrite(&Employee, sizeof(struct employeeInfo), 1, fptr_employee); break; case '2': //admin can access both regular employee page and admin page; strcpy(Employee.position, "Admin"); fwrite(&Employee, sizeof(struct employeeInfo), 1, fptr_admin); break; default: goto position; break; } printf("Do you want to add more employee Y(yes)/N(no)? "); scanf(" %c", &choice); if(choice == 'n' || choice == 'N'){ printf("Sucessfully Added"); break; } printf("\n\n"); } fclose(fptr_employee); fclose(fptr_admin); }
crashes after I enter the username and password
void log_in_employee(){ system("CLS"); char username[100]; char password[100]; fptr_employee = fopen("employee.bin", "rb"); if(fptr_employee == NULL){ printf("File not found."); exit(1); } printf("Username: "); scanf("%s", &username); printf("Password: "); scanf("%s", &password); //CHECKING USERNAME AND PASSWORD INPUT while(fread(&Employee, sizeof(Employee), 1, fptr_employee)){//admin if(strcmp(username, Employee.userName) == 0 && strcmp(password, Employee.passWord)){ employee_page(); } } fclose(fptr_employee); }
it will direct in this function if the username and password is true
void employee_page(){ system("CLS"); char choice; fptr_employee = fopen("employee.bin", "rb"); if(fptr_employee == NULL){ printf("File not found."); exit(1); } printf(" EMPLOYEE PAGE\n"); printf("______________________________________________________________\n"); printf("Personal Informaiton \n\n"); fread(&Employee, sizeof(Employee), 1, fptr_employee); printf("Name: %s\n", Employee.fullName); printf("Position: %s\n", Employee.position); printf("Birthday: %s\n ", Employee.bDay); printf("Address: %s\n", Employee.address); printf("Age: %s\n", Employee.age); printf("Mobile Number: %s\n", Employee.cpNum); printf("Sex: %s\n", Employee.sex); printf("Email: %s\n", Employee.email); printf("______________________________________________________________\n"); printf("\nRECORDS\n"); printf("Events\n"); //print printf("Attendance\n"); //print printf("Loan\n"); //print printf("Leave\n"); //print printf("\nEMPLOYEE MENU \n"); printf("1. Request Letter\n"); printf("2. Exit\n"); option: printf("Enter the number:"); scanf("%s", &choice); switch(choice){ case '1': //REQUEST FORM request_form(); break; goto option; case '2': main(); default: printf("Invalid Input"); break; goto option; } fclose(fptr_employee); }
https://stackoverflow.com/questions/65756957/login-form-crashes January 17, 2021 at 11:08AM
没有评论:
发表评论