Thread: Test condition problem,Please help!!!

  1. #1
    Registered User
    Join Date
    Feb 2012
    Location
    Trinidad & Tobago
    Posts
    43

    Test condition problem,Please help!!!

    my test condition in the "ADD MENU" function
    Code:
    if( (pos = search(ID) )  == -1)

    isn't working, thus two profile with the same ID can be added(which im trying to prevent)
    I dont understand why becouse it works in my "EDIT" function and "DELETE" function


    Just to make debugging faster:


    the "search" function returns -1 if an ID doen't esist






















    Code:
    #include <stdio.h>
    
    
    struct employee
    {
           int employee_ID;
           char employee_Name[50];
           int employee_Age;
           char employee_DateOfBirth[20];
           char employee_Address[70];
           char employee_TelNo[20];
           float emplayee_Salary;
           
    }employee_array[50]; 
    
    
    
    
    void selection_sort();
    int search();
    void add_employee();
    void edit_employee();
    void delete_employee();
    void print();
    void print_profiles();
    void print_profile();
    void print_out();
    void getstring();
    
    
    int main(void){
        
        int i,option;
        
        for(i=0;i<50;i++)
              employee_array[i].employee_ID = 0;  
              
        printf("\t\t-------------------------------------------------\n");
        printf("\t\tThis program provides the functionality  \n");
        printf("\t\tto interface with an employee information system.\n");
        printf("\t\t-------------------------------------------------\n\n");         
        
        do{
            printf("\n\n\n\n");
            printf("\t\t ----------------------------------------------\n");
            printf("\t\t|                   MAIN MENU                  |\n");
            printf("\t\t ----------------------------------------------\n\n");
            printf("\t\t[ 1 ]   -   To enter an employee's profile \n");
            printf("\t\t[ 2 ]   -   To edit an employee's profile \n");
            printf("\t\t[ 3 ]   -   To delete an employee's profile \n");
            printf("\t\t[ 4 ]   -   To print an employee's profile \n");
            printf("\t\t[ 0 ]   -   To exit \n\n");
            printf("Please choose an option:\t");
            scanf("%d",&option);  
            switch(option)
                       {
                           case 1: add_employee();
                           break;
                           case 2: edit_employee();
                           break;
                           case 3: delete_employee();
                           break;
                           case 4: print();
                           break;
                           case 0: 
                           break;
                           default:
                           printf("\n\nIncorrect Entry!,Please press enter and Try again!\n\n");
                           getch();
                           break;
                       }   
        }
        while(option != 0);
    }
    
    
    void getstring(char str[] ){ // damn fix this function's glitch
         
         int x=0;
         char ch;
         
         ch=getchar();
         while( (ch=getchar()) !='\n' ){
                str[x++]=ch;
         }
         str[x]= '\0'; 
    }
    
    
    void selection_sort(){
         
         struct employee temp;
         int i,j;
         
         for(i=0;i<50;i++){
              for(j=i+1;j<50;j++){
                   if(employee_array[j].employee_ID < employee_array[i].employee_ID)
                   {
                          temp = employee_array[j];
                          employee_array[j] =  employee_array[i];
                          employee_array[i] = temp;
                   }
              }
         }
         return ;
    }
    
    
    int search(int ID){
         int mid;
         int first = 0;
         int last = 50;
    
    
         while(first <= last)
         {
             mid = (first + last)/2;
             if(employee_array[mid].employee_ID == ID)
                 return (mid);
             if(ID < employee_array[mid].employee_ID)
                last = mid - 1;
             else
                first = mid +1;
         }
         return -1;
    }
    
    
     void add_employee(){  //fix this/ ........ing glitch
          
          int pos, ID, age;
          float salary;
          int index = 0;
          char name[50];
          char address[50];
          char telno[50];
          char DOB[20];
          
          printf("\n\n\n\n");
          printf("\t\t ----------------------------------------------\n");
          printf("\t\t|                   ADD MENU                   |\n");
          printf("\t\t ----------------------------------------------\n\n"); 
          printf("\t\tEnter[ 0 ]  -  To return to main menu:\t");
          printf("\n\n\nEnter employee's:\n");
          printf("----------------");
          printf("\n\nID-----------------------: ");
          scanf("%d",&ID);
          
          
              while( ID !=0)
              {
                     if( (pos = search(ID) )  == -1)
                     {
                         employee_array[index].employee_ID = ID;
                         printf("\nName---------------------: ");
                         getstring(name);
                         strcpy(employee_array[index].employee_Name, name);
                         printf("\nDate Of Birth------------: ");
                         getstring(DOB);
                         strcpy(employee_array[index].employee_DateOfBirth, DOB);
                         printf("\nAddress------------------: ");
                         getstring(address);
                         strcpy(employee_array[index].employee_Address, address);
                         printf("\ncontact------------------: ");
                         getstring(telno);
                         strcpy(employee_array[index].employee_TelNo, telno);
                         printf("\nAge----------------------: ");
                         scanf("%d",&age);
                         employee_array[index].employee_Age = age;
                         printf("\nSalary-------------------: ");
                         scanf("%f",&salary);
                         employee_array[index].emplayee_Salary = salary;
                         index++;
                     }
                     else
                         printf("\n\nA profile containing this ID already exist\n");
                     printf("\n\n\nEnter employee's:\n");
                     printf("----------------");
                     printf("\n\nID-----------------------: ");
                     scanf("%d",&ID);
              }
              selection_sort();
     }
    
    
    
    
    void print(){
          
          int option;
        
        do{
            printf("\n\n\n\n");
            printf("\t\t ------------------------------------------------\n");
            printf("\t\t|                   PRINT MENU                   | \n");
            printf("\t\t ------------------------------------------------\n\n");
            printf("\t\t[ 1 ]   -   to print all profiles \n");
            printf("\t\t[ 2 ]   -   to print a specific profile \n");
            printf("\t\t[ 0 ]   -   to return to main menu \n\n");
            printf("Please choose an option:\t");
            scanf("%d",&option);  
            switch(option)
                       {
                           case 1: print_profiles();
                           break;
                           case 2: print_profile();
                           break;
                           case 0: 
                           break;
                           default:
                           printf("\n\nIncorrect Entry!,Please press enter and Try again!\n\n");
                           getch();
                           break;
                       }   
        }while(option !=0);
     }
     
     
    void print_profiles(){
         
         int pos,count;
         count = pos = 0;
         
         printf("\n\n\n\n");
         printf("\t\t ----------------------------------------------------\n");
         printf("\t\t|                   SUB PRINT MENU                   |\n");
         printf("\t\t ----------------------------------------------------\n\n");
         for(;pos<50;pos++)
         {
             if(employee_array[pos].employee_ID != 0)
                 print_out(pos);
             
         }
         for(pos=0;pos<50;pos++)
         {
             if(employee_array[pos].employee_ID == 0)
                 count++;
         }
         if(count == 50)
         {
             printf("\n\n\nNo profile currently exist\n\n");
             printf("Plesae press enter to continue\n");  
             getch();
         } 
    }
    
    
    
    
    void print_profile(){
         
         int ID,pos;
         
          printf("\n\n\n\n");
          printf("\t\t -----------------------------------------------\n");
          printf("\t\t|                   SUB PRINT MENU                   |\n");
          printf("\t\t -----------------------------------------------\n\n"); 
          printf("\t\tEnter[ 0 ]  -  To return to print menu");
          printf("\n\n\nEnter employee's:\n");
          printf("----------------");
          printf("\n\nID-----------------------: ");
          scanf("%d",&ID);
         while(ID != 0)
         {
             if( (pos= search(ID) ) != -1)
                 print_out(pos);
             else
                 printf("\n\nemployee's profile could not be found\n\n");
             printf("\n\n\nEnter employee's:\n");
             printf("----------------");
             printf("\n\nID-----------------------: ");
             scanf("%d",&ID);    
         }
    }
    
    
    
    
    void print_out(int pos){
         
         printf("\n\n\nName                ---------------    :    %s",employee_array[pos].employee_Name);
         printf("\n\nAge                 ---------------    :    %d",employee_array[pos].employee_Age);
         printf("\n\nDate Of Birth       ---------------    :    %s",employee_array[pos].employee_DateOfBirth);
         printf("\n\nAddress             ---------------    :    %s",employee_array[pos].employee_Address);
         printf("\n\nTelephone Number    ---------------    :    %s",employee_array[pos].employee_TelNo);
         printf("\n\nSalary              ---------------    :    $%.2f\n\n\n",employee_array[pos].emplayee_Salary);
    }
    
    
    
    
     void edit_employee(){
          
          int pos,ID, age;
          float salary;
          char name[50];
          char address[50];
          char telno[50];
          char DOB[20];
          
          printf("\n\n\n\n");
          printf("\t\t -----------------------------------------------\n");
          printf("\t\t|                   EDIT MENU                   |\n");
          printf("\t\t -----------------------------------------------\n\n"); 
          printf("\t\tEnter[ 0 ]  -  To return to main menu\t");
          printf("\n\n\nEnter employee's:\n");
          printf("----------------");
          printf("\n\nID-----------------------: ");
          scanf("%d",&ID);
          while(ID != 0)
          {
                   if( (pos = search(ID)) != -1)
                   {
                       employee_array[pos].employee_ID = ID;
                       printf("\nName---------------------: ");
                       getstring(name);
                       strcpy(employee_array[pos].employee_Name, name);
                       printf("\nAge----------------------: ");
                       scanf("%d",&age);
                       employee_array[pos].employee_Age = age;
                       printf("\nDate Of Birth------------: ");
                       getstring(DOB);
                       strcpy(employee_array[pos].employee_DateOfBirth, DOB);
                       printf("\nAddress------------------: ");
                       getstring(address);
                       strcpy(employee_array[pos].employee_Address, address);
                       printf("\ncontact------------------: ");
                       getstring(telno);
                       strcpy(employee_array[pos].employee_TelNo, telno);
                       printf("\nSalary-------------------: ");
                       scanf("%f",&salary);
                       employee_array[pos].emplayee_Salary = salary;
                   }
                   else
                       printf("\n\nNo profiles containing this ID was found");
                   printf("\n\n\nEnter employee's:\n");
                   printf("----------------");
                   printf("\n\nID-----------------------: ");
                   scanf("%d",&ID);
          }
          selection_sort();
    }
     
     
    void delete_employee(){
          
          int pos,ID;
          
          printf("\n\n\n\n");
          printf("\t\t -------------------------------------------------\n");
          printf("\t\t|                   DELETE MENU                   |\n");
          printf("\t\t -------------------------------------------------\n\n"); 
          printf("\t\tEnter[ 0 ]  -  To return to main menu\t");
          printf("\n\n\nEnter employee's:\n");
          printf("----------------");
          printf("\n\nID-----------------------: ");
          scanf("%d",&ID);
          while(ID != 0)
          {
                   if( (pos = search(ID)) != -1)
                   {
                       employee_array[pos].employee_ID = 0;
                       printf("\n\nEmployee's ID have successfully been deleted\n");
                   }
                   else
                       printf("\n\nEmployee's profile does not exist\n");
                   printf("\n\n\nEnter employee's:\n");
                   printf("----------------");
                   printf("\n\nID-----------------------: ");
                   scanf("%d",&ID);
          }
          selection_sort();
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Your binary search needs work. It always results in -1, which would explain why it "works" when you want to add employees, because the program will always add, but not when you want to edit them, because the employees are always not found. Your employee array is global... which means if you had a few interesting IDs, they'd be among a bunch of zero IDs that affect the search. For example (23, 24, 0, 0, 0 ... ). A zero will be found before any of the interesting ids, and the search ends up proceeding in a wrong direction. The array is sorted if you restrict the range to the number of elements you are actually using but if the search space is ever not sorted binary search will not work.

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Brazil
    Posts
    58
    Sorry, but I've never seen an if clause being used to test an expression like this.
    How does it work?

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The search function is called first:

    (pos = search(ID))

    Then that result becomes part of the larger expression in the if condition, so it's as if you wrote:

    pos = search(ID);
    if (pos == -1)

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Brazil
    Posts
    58
    Oh, very nice, didn't know it could be used that way.
    Thanks!

  6. #6
    Registered User
    Join Date
    Feb 2012
    Location
    Trinidad & Tobago
    Posts
    43
    @whiteflags

    please explain further - you lost me because i thought my "selection sort" function allows the binary search to work,and the "search" doesn't always return -1 it returns the position of the ID if it's found. Also the "edit" function works just fine, that's why i'm confused as to why "add" isn't working.


  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    A binary search assumes also assumes a sorted array.

    Editing an entry does not affect the sorting. Adding or deleting an entry does, so the array needs to be sorted immediately after adding or deleting an entry.

    I suspect your testing of delete_employee() is insufficient, because (if testing was sufficient) you would have found the same problems with delete_employee() as with add_employee() .....
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Registered User
    Join Date
    Feb 2012
    Location
    Trinidad & Tobago
    Posts
    43
    @grumpy

    i have tested it properly, the reason as to why one works and the other doesn't? is beyond my knowledge, that's why in here now

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    He explained why it worked:

    Editing an entry does not affect the sorting.
    Even if you get lucky and the right thing happens for the wrong reasons, the logic is still wrong. You must sort before you search... in this case it might be better if search() calls selection_sort() instead of other client code.

    please explain further - you lost me because i thought my "selection sort" function allows the binary search to work,and the "search" doesn't always return -1 it returns the position of the ID if it's found. Also the "edit" function works just fine, that's why i'm confused as to why "add" isn't working.
    I was afraid I would get it mixed up. I thought I had explained myself adequately but apparently not.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Shinzu911 View Post
    i have tested it properly, the reason as to why one works and the other doesn't? is beyond my knowledge, that's why in here now
    Your notion of "proper" testing is clearly less complete than mine.

    By a cursory visual examination of the code, I can see that delete_employee() function will not always function correctly if there are 4 elements in the array, all with positive IDs, and an attempt is made to delete (one after the other) the two in the middle. The reason is that removing the first leaves the array unsorted (there is a zero ID between two positive ones) which potentially messes up the search for the second element to be removed. Then, having "removed" the two elements, the array is sorted.

    The reason you BELIEVE one works is that you didn't test it properly. The reason the other is failing is that you happened to stumble on a test case that exercised the flaw. In fact, both the add_employee() and delete_employee() exhibit a similar flaw.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-07-2012, 10:00 AM
  2. condition problem
    By cubanoemg in forum C Programming
    Replies: 10
    Last Post: 02-07-2011, 03:33 PM
  3. Replies: 7
    Last Post: 07-18-2005, 08:43 AM
  4. Repeat condition problem
    By kumar14878 in forum C Programming
    Replies: 3
    Last Post: 05-04-2005, 07:43 AM
  5. Math Test Problem Disagreement
    By orbitz in forum A Brief History of Cprogramming.com
    Replies: 29
    Last Post: 02-28-2003, 04:27 PM