Introduction
In this lab, we will create a C++ program that stores and displays employee information using structures. This program will help us understand how structures can be used to store data with different data types. We will use an array of structures to store multiple records of employee information and then display it in a formatted manner.
Define a Structure for Employee Information
We will first define a structure that will hold the information of an employee, including their ID, name, salary, and department.
struct employee {
int empID;
char name[50];
int salary;
char department[50];
};
Here, we have defined a structure named 'employee' with four member variables of different data types. The first member variable 'empID' represents the unique identification number of an employee, second variable 'name' stores the name of the employee, third variable 'salary' holds the amount of salary earned by the employee, and the fourth variable 'department' holds the name of the department in which the employee works.
Store Employee Information
We will now create an array of structures to store the information of multiple employees.
struct employee emp[3] = {
{ 1 , "Harry" , 20000 , "Finance" },
{ 2 , "Sally" , 50000 , "HR" },
{ 3 , "John" , 15000 , "Technical" }
};
In this code block, we have created an array of three structures 'emp' and initialized it with the information of three employees. Each structure in the array holds four values: ID, name, salary, and department. You can modify these values or add more employees to the array if needed.
Display Employee Information
We will now display the information of each employee in a formatted manner using a for loop.
for(int i=0; i<3;i++) {
cout<<"Employee ID: "<<emp[i].empID<<endl;
cout<<"Name: "<<emp[i].name<<endl;
cout<<"Salary: "<<emp[i].salary<<endl;
cout<<"Department: "<<emp[i].department<<endl;
cout<<endl;
}
In this code block, we have used a for loop to iterate through the array of structures and display the information of each employee on a new line. The information is formatted according to the labels 'Employee ID:', 'Name:', 'Salary:', and 'Department:'. The 'endl' at the end of each cout statement is used to move the output to a new line.
Compile and Execute the Program
Now, save the changes we made to file main.cpp. In the terminal, navigate to the project directory ~/project and run the following command to compile and execute the program:
g++ main.cpp -o main && ./main
This should output the information of each employee in the following format:
Employee ID: 1
Name: Harry
Salary: 20000
Department: Finance
Employee ID: 2
Name: Sally
Salary: 50000
Department: HR
Employee ID: 3
Name: John
Salary: 15000
Department: Technical
Summary
In this lab, we learned how to use structures to store and display employee information in a C++ program. We created a structure to hold the information of an employee, used an array of structures to store multiple records of employee information, and then displayed it in a formatted manner. This lab can be extended to include additional information or functionality as per your needs.



