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();
}