This is a mini c project I working right now. I try to login after I register a new account,but even I input the correct information still fail to login, can't find the error. The code is quite a lots, but I tried my best to minimize.
Code:
This is the main interface when the console run, for choosing login either register.If you are first-time login user, you have to register a new account before login.
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<windows.h>
#include<string.h>
void customer();
void customerres();
void customerlogin();
void fordelay();
int i, j;
int main_exit;
struct date
{
int month, day, year;
};
struct
{
int acc_no, age;
char password[20];
char repassword[20];
char name[60];
char user[60];
char address[60];
char gender[15];
char acc_type[10];
double phone;
float amt;
struct date dob;
struct date deposit;
struct date withdraw;
}add, upd, check;
void fordelay(int j)
{
int i, k;
for (i = 0;i < j;i++)
k = i;
}
void main()
{
system("cls");
int choose;
printf("\n1. Login\n2. Register\n\n\nEnter your choice:");
scanf("%d", &choose);
switch (choose)
{
case 1:
customerlogin();
break;
case 2:
customerres();
break;
}
}
And this is the register feature, I add two feature which are check the availability of account number, and check the re-type password match or not. For checking the account number availability, I use POINTER to do it. After input the rest of user's information, the program will save it into a txt file.
void customerres()
{
int checkpassword;
FILE* ptr;
ptr = fopen("record.txt", "a+");
account_no:
system("cls");
printf("Register New Account");
printf("\n\n\nEnter today's date(dd/mm/yyyy):");
scanf("%d/%d/%d", &add.deposit.day, &add.deposit.month, &add.deposit.year);
printf("\nEnter the Account Number:");
scanf("%d", &check.acc_no);
while (fscanf(ptr, "%d %s %s %s %s %d/%d/%d %d %s %s %lf %s %f %d/%d/%d\n", &add.acc_no, add.password, add.repassword, add.user, add.name, &add.dob.month, &add.dob.day, &add.dob.year, &add.age, add.address, add.gender, &add.phone, add.acc_type, &add.amt, &add.deposit.month, &add.deposit.day, &add.deposit.year) != EOF)
{
if (check.acc_no == add.acc_no)
{
printf("Account no. already in use!");
fordelay(1000000000);
goto account_no;
}
}
add.acc_no = check.acc_no;
printf("\nEnter the Username:");
scanf("%s", add.user);
checkpassword = 0;
while (checkpassword == 0)
{
password:
printf("\nEnter the password:");
scanf("%s", add.password);
printf("\nRe-enter the password:");
scanf("%s", add.repassword);
if (strcmp(add.password, add.repassword) == 0)
{
checkpassword = 1;
printf("Password match!\n");
}
else
{
printf("Password don't match!\n");
fordelay(1000000000);
goto password;
}
}
printf("\nEnter your name:");
scanf("%s", add.name);
printf("\nEnter the Date Of Birth(dd/mm/yyyy):");
scanf("%d/%d/%d", &add.dob.day, &add.dob.month, &add.dob.year);
printf("\nEnter the age:");
scanf("%d", &add.age);
printf("\nEnter the address:");
scanf("%s", add.address);
printf("\nEnter the gender:");
scanf("%s", add.gender);
printf("\nEnter the phone number: ");
scanf("%lf", &add.phone);
printf("\nEnter the amount to deposit:$");
scanf("%f", &add.amt);
printf("\nType of account:\n\t#Saving\n\t#Current\n\tEnter your choice:");
scanf("%s", add.acc_type);
fprintf(ptr, "%d %s %s %s %s %d/%d/%d %d %s %s %lf %s %f %d/%d/%d\n", &add.acc_no, add.password, add.repassword, add.user, add.name, add.dob.month, add.dob.day, add.dob.year, add.age, add.address, add.gender, add.phone, add.acc_type, add.amt, add.deposit.month, add.deposit.day, add.deposit.year);
fclose(ptr);
printf("\nAccount created successfully!");
add_invalid:
printf("\n\n\n\t\tEnter 1 to go to the main menu and 0 to exit:");
scanf("%d", &main_exit);
system("cls");
if (main_exit == 1)
{
main();
}
else if (main_exit == 0)
{
exit(0);
}
else
{
printf("\nInvalid!\a");
goto add_invalid;
}
}
In this code, I did a function to check do the user has entered the correct information. If the account number, username, and password are match, it will log in successfully, but I failed here.
void customerlogin()
{
system("cls");
FILE* ptr;
ptr = fopen("record.txt", "r");
printf("\nAccount number: ");
scanf("%d", &check.acc_no);
printf("Username: ");
scanf("%s", check.name);
printf("Password: ");
scanf("%s", check.password);
int checkcredential, choose, findacct, login;
choose = 0;
checkcredential = 0;
findacct = 0;
login = 0;
while (fscanf(ptr, "%d %s %s %s %s %d/%d/%d %d %s %s %lf %s %f %d/%d/%d\n", &add.acc_no, add.password, add.repassword, add.user, add.name, &add.dob.month, &add.dob.day, &add.dob.year, &add.age, add.address, add.gender, &add.phone, add.acc_type, &add.amt, &add.deposit.month, &add.deposit.day, &add.deposit.year) != EOF)
{
if (check.acc_no == add.acc_no)
{
if (strcmp(check.name, add.name) == 0)
{
if (strcmp(check.password, add.password) == 0)
{
fclose(ptr);
login = 1;
customer(check.acc_no);
}
else
{
checkcredential = 1;
}
}
else
{
checkcredential = 1;
}
}
else
{
findacct = 1;
}
}
fclose(ptr);
if (login == 0)
{
if (findacct == 1 || checkcredential == 1)
{
printf("\nSorry, wrong credentials!\n\nEnter 1 to try again and 0 to exit:");
scanf("%d", &main_exit);
system("cls");
if (main_exit == 1)
{
customerlogin();
}
else if (main_exit == 0)
{
exit(0);
}
}
}
}
void customer()
{
printf("/n Login Successful!");
}
Thanks for any correction about my code, I appreciated it.
What I have tried:
I try many ways to edit my code but still failed, seem like I didn't really catch the error correctly...