i'm trying to make a login page by using binary search tree method. The user ID is stored in a notepad, so i've to transfer it first to the structure inside my program. it has already worked, everytime i login using username and password that is written on the notepad. But there's some error, every time i write username that is not in the notepad, the program crashed.
do{
char user[100];
char pass[100];
printf("Input your username : ");
scanf("%[^\n]",&user);fflush(stdin);
printf("Input your password : ");
scanf("%[^\n]",&pass);fflush(stdin);
printf("\n\n");
searchID(root,user,pass);
if(flagID==0 || flagPass==0)
{
printf("Invalid Username/Password\n");
}
else if(flagID==1 && flagPass==1)
{
printf("Login Successful\n");
}
}while(flagID==0 || flagPass==0);
that is the code for login page
and this is the searchID function
if(temp->left== NULL && temp-> right == NULL && strcmp(temp->pass,pass)!=0)
{
printf("Username Not Found\n");
flagID=0;
}
else if(strcmp(temp->user,user)==0)
{
if(strcmp(temp->pass,pass)==0)
{
flagPass=1;
}
else
{
flagPass=0;
}
flagID=1;
}
else if(strcmp(temp->user,user)<0)
{
searchID(temp->left,user,pass);
}
else if(strcmp(temp->user,user)>0)
{
searchID(temp->right,user,pass);
}
if(temp->left== NULL && temp-> right == NULL && strcmp(temp->pass,pass)!=0)
{
printf("Username Not Found\n");
flagID=0;
}
i don't understand why this part seems doesn't work. as far as i'm concerned the logic is already right
any thoughts?
the whole code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
void clrscr();
void readFile();
void insertID(struct login **temp,char* user,char* pass, int score,char* jabatan);
void inOrder(struct login *root);
void searchID(struct login *temp,char* user,char* pass);
void menu();
int count =0;
int flagID=0;
int flagPass=0;
struct login{
char user[100];
char pass[100];
int score;
char jabatan[10];
struct login *left,*right;
}*root,*curr;
int main()
{
readFile();
menu();
getchar();
clrscr();
getchar();
return 0;
}
void menu()
{
clrscr();
do{
char user[100];
char pass[100];
printf("Input your username : ");
scanf("%[^\n]",&user);fflush(stdin);
printf("Input your password : ");
scanf("%[^\n]",&pass);fflush(stdin);
printf("\n\n");
searchID(root,user,pass);
if(flagID==0 || flagPass==0)
{
printf("Invalid Username/Password\n");
}
else if(flagID==1 && flagPass==1)
{
printf("Login Successful\n");
}
}while(flagID==0 || flagPass==0);
}
void clrscr()
{
for(int i=0;i<25;i++)
{
printf("\n");
}
}
void readFile()
{
char temp_user[100];
char temp_pass[100];
int temp_score;
char temp_jabatan[20];
FILE *f ;
f=fopen("user.txt","r");
while(!feof(f))
{
fscanf(f,"%[^|]|%[^|]|%d|%[^\n]\n",&temp_user,&temp_pass,&temp_score,&temp_jabatan);
insertID(&root,temp_user,temp_pass,temp_score,temp_jabatan);
count++;
}
fclose(f);
}
void insertID(struct login **temp,char* user,char* pass, int score,char* jabatan)
{
if(*temp==NULL)
{
curr=(struct login*)malloc(sizeof(struct login));
strcpy(curr->user,user);
strcpy(curr->pass,pass);
curr->score=score;
strcpy(curr->jabatan,jabatan);
*temp=curr;
curr->left=curr->right=NULL;
}
else
{
if(curr->score<score)
{
insertID(&(*temp)->right,user,pass,score,jabatan);
}
else if(curr->score>score)
{
insertID(&(*temp)->left,user,pass,score,jabatan);
}
}
}
void searchID(struct login *temp,char* user,char* pass)
{
if(temp->left== NULL && temp-> right == NULL && strcmp(temp->pass,pass)!=0)
{
printf("Username Not Found\n");
flagID=0;
}
else if(strcmp(temp->user,user)==0)
{
if(strcmp(temp->pass,pass)==0)
{
flagPass=1;
}
else
{
flagPass=0;
}
flagID=1;
}
else if(strcmp(temp->user,user)<0)
{
searchID(temp->left,user,pass);
}
else if(strcmp(temp->user,user)>0)
{
searchID(temp->right,user,pass);
}
}
void inOrder(struct login *root)
{
if(root)
{
inOrder(root->left);
printf("%s - %d\n",root->user,root->score);
inOrder(root->right);
}
}
user.txt
rio|io|14|admin
hermanto|manto|50|member
andry kurniawan|awan|56|member
charles sutanto|char|25|member
dianto erwin|erwin|28|admin
kuki karuna|kuki|125|admin
izra|iz|9|member
timothy agustian|tim|15|admin
susi|sus|4|member
ryan purnama|pur|2|member
stephanus sujatmoko|moko|19|member