0

Hello, good evening. :)

I'm having a hard time with my "Searching" codes. :(

The problem is: When I try to search the employee number after registering, my program will print "EMPLOYEE NUMBER NOT FOUND".

I don't know where's the culprit. :(

Here's the whole codes:

#include <stdio.h>
#include <conio.h>
#include <string.h>

int payroll_system_menu();
int search_employees_menu();

int main()
{
 char fname[100][100],lname[100][100],sex[100][100],address[100][100],dob[100][100];
 char *day[5]={"Monday","Tuesday","Wednesday","Thursday","Friday"};
 int empno[100],rate[100];
 int payrollmenu,searchmenu;
 int i=0,j=0,f=0,found=0;
 int srchemp,payemp;
 float timeinhour[5],timeinminute[5],timeouthour[5],timeoutminute[5];
 float totalhour,totalmin,workhour;
 float sss,cashad,others,gsalary,salary,salrate,tax;
 totalhour = 0;
 totalmin = 0;
 clrscr();
 do
 {
  payrollmenu = payroll_system_menu();
  if(payrollmenu == 1)
  {
   printf("\n\t\t******************************************");
   printf("\n\t\t********* REGISTER NEW EMPLOYEE **********");
   printf("\n\t\t******************************************");
   printf("\n\n\t\t Employee No.: ");
   scanf("%d",&empno[i]);
   printf("\t\t First Name: ");
   scanf("%s",&fname[j]);
   printf("\t\t Last Name: ");
   scanf("%s",&lname[j]);
   printf("\t\t Date of Birth: ");
   scanf("%s",&dob[j]);
   printf("\t\t Sex: ");
   scanf("%s",&sex[j]);
   printf("\t\t Address: ");
   scanf("%s",&address[j]);
   printf("\t\t Rate/Hour: ");
   scanf("%d",&rate[i]);
   printf("\n\t\t INFOMATION HAS BEEN SAVED. THANK YOU!!!");
   i++;
   j++;
   getch();
  }
  if(payrollmenu == 2)
  {
   printf("\n\t\t******************************************");
   printf("\n\t\t************* SEARCH EMPLOYEE ************");
   printf("\n\t\t******************************************");
   printf("\n\n\t\t Enter Employee Number: ");
   scanf("%d",&srchemp);
   for(i=0;i<=100;i++)
   {
    if(empno[i]==srchemp)
    {
     found=1;
    }
   }
   if(found==1)
   {
    printf("\n\t\t EMPLOYEE NUMBER FOUND!\n");
   }
   else
   {
    printf("\n\t\t EMPLOYEE NUMBER NOT FOUND!");
   }
   getch();
  }
  if(payrollmenu == 3)
  {
   printf("\n\t\t******************************************");
   printf("\n\t\t************ CREATE PAYROLL **************");
   printf("\n\t\t******************************************");
   printf("\n\n\t\t Enter Employee Number: ");
   scanf("%d",&payemp);
   for(i=0;i<=100;i++)
   {
    if(payemp != empno[i])
    {
     printf("\n\t\t EMPLOYEE %d NOT FOUND!\n",payemp);
    }
   }
	while(f<=4)
	{
	 printf("\n\t\t Time In for %s: ",day[f]);
	 scanf("%f:%f",&timeinhour[f],&timeinminute[f]);
	 if((timeinhour[f]>=8) && (timeinhour[f]<12))
	 {
		if(timeinhour[f]>8)
		{
		totalhour = totalhour + (timeinhour[f] - 8);
		totalmin = totalmin + timeinminute[f];
		}
		if((timeinhour[f]==8) && (timeinminute[f]>0))
		{
       totalmin = totalmin + timeinminute[f];
      }
	 }
	 if(timeinhour[f]>=12)
	 {
		totalhour = totalhour + 4;
		if(timeinhour[f]==13)
		{
       totalmin = totalmin + timeinminute[f];
		}
      if(timeinhour[f]>13)
		{
		 totalhour = totalhour + (timeinhour[f] - 13);
		 totalmin = totalmin + timeinminute[f];
		}
	 }
	 printf("\t\t Time Out for %s: ",day[f]);
	 scanf("%f:%f",&timeouthour[f],&timeoutminute[f]);
	 if(timeouthour[f]<=16)
	 {
		if(timeouthour[f]<16)
		{
		 totalhour = totalhour + (16 - timeouthour[f]);
		 totalmin = totalmin + (60 - timeoutminute[f]);
		}
		if(timeouthour[f]==16)
		{
       totalmin = totalmin + (60 - timeoutminute[f]);
	   }
    }
    f++;
	 printf("\t\t------------------------------------------");
	}
   printf("\n\t\t DEDUCTIONS:");
   printf("\n\t\t    SSS: ");
   scanf("%f",&sss);
   printf("\t\t    Cash Advance: ");
   scanf("%f",&cashad);
   printf("\t\t    Tax: ");
   scanf("%f",&tax);
   printf("\t\t    Others: ");
   scanf("%f",&others);
	workhour = 40 - (totalhour + totalmin/60);
	gsalary = salrate * workhour;
	tax = gsalary * 0.010;
	salary = gsalary - tax;
	printf("\n\t\t------------------------------------------\n");
	printf("\n\t\t|                  PAYROLL               |\n");
	printf("\n\t\t------------------------------------------\n");
	printf("\t\tName                      : %s %s\n",fname,lname);
	printf("\t\tNo. of hours worked       : %.0f\n",workhour);
	printf("\t\tGross Salary              : %.2f\n",gsalary);
	printf("\t\tNet Salary                : %.2f\n",salary);
	printf("\t\t------------------------------------------\n");
  }
 }while(payrollmenu != 4);
 return 0;
}

int payroll_system_menu()
{
 int cho;
 printf("\n\t\t******************************************");
 printf("\n\t\t******* P A Y R O L L   S Y S T E M ******");
 printf("\n\t\t******************************************");
 printf("\n\n\t\t [1] Register New Employees\n");
 printf("\t\t [2] Search Employees\n");
 printf("\t\t [3] Create Payroll\n");
 printf("\t\t [4] Exit\n");
 printf("\n\t\t Enter your choice: ");
 scanf("%d",&cho);
 return cho;
}

Hope you can help me. :(

2
Contributors
1
Reply
3
Views
6 Years
Discussion Span
Last Post by PsychoLogic
0

When you add a new employee you don't set the value of i (and j, I don't get why you have two different values here) to the appropriate position in the arrays. This is fine for a single insert if that's all you were going to do, but will cause problems when inserting a second value, or after you run a search which will leave i set to 101 and you'll overrun the buffer. (You are already going one past the end of the array with the for loop in your search - this is bad).

You might be better off using local counter parameters for your loops instead of the global ones, this would help you avoid some of these types of problems.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.