Home »
Code Snippets »
C/C++ Code Snippets
C program to read and print a company’s information along with departments and employees record
This program is asked by Shalini through comment on 01/12/2016.
Question
In a company there are many departments under different managers but i want to know the details of one manager and the teams present under him and the employees present in the each team.And how to delete and insert an employees.
Answer
In this program we are going to read and print the company’s details along with the departments and employees records under each department.
To implement this program we are using three structures: company, department and employee
A company may have more than one department and a department may have more than one employee.
C program for company management with departments and employees
#include <stdio.h>
struct employee{
char name[30];
int id;
};
struct department{
struct employee emp[10];
char deptName[30];
char managerName[30];
};
struct company{
struct department dept[5];
};
int main()
{
struct company comp;
int noDept; //number of departments
int noEmp; //number of employees in a department
int i,j;
char temp;
printf("Enter total number of departments: ");
scanf("%d",&noDept);
for(i=0;i<noDept;i++)
{
printf("\nEnter department name: ");
scanf("%c",&temp);
scanf("%[^\n]s",comp.dept[i].deptName);
printf("Enter manager name: ");
scanf("%c",&temp);
scanf("%[^\n]s",comp.dept[i].managerName);
printf("\nEnter no of employees in a department: ");
scanf("%d",&noEmp);
for(j=0;j<noEmp;j++)
{
printf("Enter employee id: ");
scanf("%d",&comp.dept[i].emp[j].id);
printf("Enter employee name: ");
scanf("%c",&temp);
scanf("%[^\n]s",comp.dept[i].emp[j].name);
}
}
//print all details
printf("\n\nEntered details are:\n");
for(i=0;i<noDept;i++)
{
printf("Department: %s, Manager: %s\n",comp.dept[i].deptName,comp.dept[i].managerName);
printf("EMP ID \t EMP NAME\n");
for(j=0;j<noEmp;j++)
printf("%5d \t %s\n",comp.dept[i].emp[j].id,comp.dept[i].emp[j].name);
}
return 0;
}
Output
Enter total number of departments: 2
Enter department name: NETWORK
Enter manager name: Alex Thomas
Enter no of employees in a department: 2
Enter employee id: 101
Enter employee name: PRIYA KAUSHAL
Enter employee id: 102
Enter employee name: NAVYA J.
Enter department name: TESTING
Enter manager name: Mike Thomas
Enter no of employees in a department: 2
Enter no of employees in a department: 2
Enter employee id: 901
Enter employee name: HAPPY K.
Enter employee id: 902
Enter employee name: JUHI C.
Entered details are:
Department: NETWORK, Manager: Alex Thomas
EMP ID EMP NAME
101 PRIYA KAUSHAL
102 NAVYA J.
Department: TESTING, Manager: Mike Thomas
EMP ID EMP NAME
901 HAPPY K.
902 JUHI C.
In this program I am explaining to read and display a company’s information with the departments and employee under each department, you have to write code to insert and remove record and if possible try to make this program using file handling. If feel any problem you can ask again.