I have googled many times but I cannot find a concrete answer to my question/problem. I know fgets() allows it, as well as gets(). But if i do it multiple times, there's always an error. Multiple times mean, e.g. I want to ask for his first name, then his middle name, then his surname which can be 1 or more strings.
I think I got it by using gets since I can't make fgets() work, but there's some bug in my program. So, I wanna ask for a better solution than what I did. Anyway, here's the partial code:
BST *insert(BST *root, BST *temp){
if(root == NULL) root = temp; //empty
else{
temp->parent = root;
//if true i = 1, else i = 0
int i = root->employee.emnumber <= temp->employee.emnumber;
//recurse
root->child[i] = insert(root->child[i], temp);
}
return root;
}
int checknum(BST *root, int num){
if(root == NULL){
if(num <= 99999 && num >= 1) return 0;
else return 1;
}
if(root->employee.emnumber == num || num > 99999 || num < 1) return 1;
int i = root->employee.emnumber <= num;
checknum(root->child[i], num);
}
BST *add(BST *root){
int invalid = -1;
BST *temp = malloc (sizeof (BST) );
temp->child[0] = temp->child[1] = temp->parent = NULL;
printf("\n\tADD EMPLOYEE INFORMATION\n");
printf("\nEnter Employee Number: ");
scanf("%d", &(temp->employee.emnumber) );
invalid = checknum(root,temp->employee.emnumber);
if(invalid) printf("\nInvalid Input!\n(Either your input is not a 5-digit number or the employee number is already in the database)\n\n");
else{
printf("\nFull Name| First Name: ");
getchar();
gets(temp->employee.emname.fn);
//printf("%d\n", strlen(temp->employee.emname.fn));
if(strlen(temp->employee.emname.fn) > max) printf("Invalid Input!\n(Input only %d characters)\n", max);
else{
printf(" Middle Name: ");
gets(temp->employee.emname.mn);
if(strlen(temp->employee.emname.mn) > max) printf("Invalid Input!\n(Input only %d characters)\n", max);
else{
printf(" Last Name: ");
gets(temp->employee.emname.ln);
if(strlen(temp->employee.emname.ln) > max) printf("Invalid Input!\n(Input only %d characters)\n", max);
else{
printf("\nBirth Date| Month: ");
scanf("%d", &(temp->employee.bdate.month) );
printf(" Day: ");
scanf("%d", &(temp->employee.bdate.day) );
printf(" Year: ");
scanf("%d", &(temp->employee.bdate.year) );
printf("\nCurrent Address: ");
scanf("%s", temp->employee.address);
printf("\nMonthly Gross Salary: ");
scanf("%d", &(temp->employee.salary) );
printf("\nHire Date| Month: ");
scanf("%d", &(temp->employee.hired.month) );
printf(" Day: ");
scanf("%d", &(temp->employee.hired.day) );
printf(" Year: ");
scanf("%d", &(temp->employee.hired.year) );
root = insert(root, temp);
}
}
}
}
}
The bug is that if I entered an invalid number in add.Let's say I input 5 in employee number but there's employee number 5 already, the program will say that there's already employee 5 but if i print the employee list employee 5 is not there. Entering 5 again in add will not prompt you because it deleted employee 5. I do not how it is deleted since i did not call the delete function. Please someone help me.
edit: I removed checknum and did the search in my search function for delete: my code for search is:
BST *search(BST *root, int x){
int i;
if(root == NULL || root->employee.emnumber == x) return(root);
i = root->employee.emnumber <= x;
return search(root->child[i], x);
}
and add is now:
BST *add(BST *root){
BST *invalid = NULL;
BST *temp = malloc (sizeof (BST) );
temp->child[0] = temp->child[1] = temp->parent = NULL;
printf("\n\tADD EMPLOYEE INFORMATION\n");
printf("\nEnter Employee Number: ");
scanf("%d", &(temp->employee.emnumber) );
if(temp->employee.emnumber > 99999 || temp->employee.emnumber < 1)
printf("\nInvalid Input!\n(Input only 5-digit number)\n\n");
else{
invalid = search(root,temp->employee.emnumber);
if(invalid != NULL) printf("\nInvalid Input!\n(Already in the database)\n\n");
else{
/*The same codes here*/
}
}
}
I think the problem now lies on what it returns to root when it encounters an "invalid input." I call add() by root=add(root) so if it encountered a problem it points to null or something else. So what do you think must I do?
EDIT [LAST]: The problem really lies in what add() returns if it encountered the "invalid input." I changed BST *add to void add to avoid the root=add(root) then did the necessary changes in the function and now it works. Thanks for your answers, but I cannot give a check mark since I think no one answered it right. I'm now making it go with fgets().