title: Login Validation using binary Search Tree C Programming
body: <p>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.</p>
  
  <pre><code>do{
      char user[100];
      char pass[100];
      printf("Input your username : ");
      scanf("%[^\n]",&amp;user);fflush(stdin);
      printf("Input your password : ");
      scanf("%[^\n]",&amp;pass);fflush(stdin);
      printf("\n\n");
      searchID(root,user,pass);
      if(flagID==0 || flagPass==0)
      {
          printf("Invalid Username/Password\n");
      }
      else if(flagID==1 &amp;&amp; flagPass==1)
      {
          printf("Login Successful\n");
      }
  
  }while(flagID==0 || flagPass==0);
  </code></pre>
  
  <p>that is the code for login page</p>
  
  <p>and this is the searchID function</p>
  
  <pre><code>if(temp-&gt;left== NULL &amp;&amp; temp-&gt; right == NULL &amp;&amp; strcmp(temp-&gt;pass,pass)!=0)
  {
      printf("Username Not Found\n");
      flagID=0;
  }
  else if(strcmp(temp-&gt;user,user)==0)
  {
      if(strcmp(temp-&gt;pass,pass)==0)
      {
          flagPass=1;
      }
      else
      {
          flagPass=0;
      }
      flagID=1;
  }
  else if(strcmp(temp-&gt;user,user)&lt;0)
  {
      searchID(temp-&gt;left,user,pass);
  }
  else if(strcmp(temp-&gt;user,user)&gt;0)
  {
      searchID(temp-&gt;right,user,pass);
  }
  </code></pre>
  
  <hr>
  
  <pre><code>if(temp-&gt;left== NULL &amp;&amp; temp-&gt; right == NULL &amp;&amp; strcmp(temp-&gt;pass,pass)!=0)
  {
      printf("Username Not Found\n");
      flagID=0;
  }
  </code></pre>
  
  <p>i don't understand why this part seems doesn't work.
  as far as i'm concerned the logic is already right</p>
  
  <p>any thoughts?</p>
  
  <p>the whole code</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;ctype.h&gt;
  
  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]",&amp;user);fflush(stdin);
          printf("Input your password : ");
          scanf("%[^\n]",&amp;pass);fflush(stdin);
          printf("\n\n");
          searchID(root,user,pass);
          if(flagID==0 || flagPass==0)
          {
              printf("Invalid Username/Password\n");
          }
          else if(flagID==1 &amp;&amp; flagPass==1)
          {
              printf("Login Successful\n");
          }
  
      }while(flagID==0 || flagPass==0);
  }
  
  void clrscr()
  {
      for(int i=0;i&lt;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",&amp;temp_user,&amp;temp_pass,&amp;temp_score,&amp;temp_jabatan);
          insertID(&amp;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-&gt;user,user);
          strcpy(curr-&gt;pass,pass);
          curr-&gt;score=score;
          strcpy(curr-&gt;jabatan,jabatan);
          *temp=curr;
          curr-&gt;left=curr-&gt;right=NULL;
      }
      else
      {
          if(curr-&gt;score&lt;score)
          {
              insertID(&amp;(*temp)-&gt;right,user,pass,score,jabatan);
          }
          else if(curr-&gt;score&gt;score)
          {
              insertID(&amp;(*temp)-&gt;left,user,pass,score,jabatan);
          }
      }
  
  }
  
  void searchID(struct login *temp,char* user,char* pass)
  {
      if(temp-&gt;left== NULL &amp;&amp; temp-&gt; right == NULL &amp;&amp; strcmp(temp-&gt;pass,pass)!=0)
      {
          printf("Username Not Found\n");
          flagID=0;
      }
      else if(strcmp(temp-&gt;user,user)==0)
      {
          if(strcmp(temp-&gt;pass,pass)==0)
          {
              flagPass=1;
          }
          else
          {
              flagPass=0;
          }
          flagID=1;
      }
      else if(strcmp(temp-&gt;user,user)&lt;0)
      {
          searchID(temp-&gt;left,user,pass);
      }
      else if(strcmp(temp-&gt;user,user)&gt;0)
      {
          searchID(temp-&gt;right,user,pass);
      }
  }
  
  
  void inOrder(struct login *root)
  {
      if(root)
      {
          inOrder(root-&gt;left);
          printf("%s - %d\n",root-&gt;user,root-&gt;score);
          inOrder(root-&gt;right);
      }
  }
  </code></pre>
  
  <p>user.txt</p>
  
  <pre><code>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
  </code></pre>