What Is Structure ?
The structure is a user-defined data type in C++. The structure is used to represent a record. Suppose you want to store a record of Student which consists of student name, address, roll number and age. You can define a structure to hold this information.
Defining a structure?
struct keyword is used to define a structure. struct define a new data type which is a collection of different type of data.
Syntax:-
struct structure_name
{
Statement...........1
Statement...........2
Statement...........3
.
.
.
Statement...........n
};
Note:- here statement refers variables and member of the structure.
Logic:-
So as we get some basic idea about structure now we define the structure and structure name is Employee( you can change ) and its variable is ID, Name, Age, and Salary, as you can see that now our structure looks like above syntax of the structure. Now the next step is to take the user input and store the user input value in structure with the help of 'For Loop' (you can use while and do while loops). at the end we have to print the value stored in structure to console this will help to check your structure is performing well or not. Here I use iomanip for manipulating data better like while printing the data of structure systematic manner and the setw() is used to print the data in manner
Setw()
Sets the field width to be used on output operations, it comes under the iomainp header file.
Output:
Enter The Number of Employee
5
Enter details of 1 Employee
Enter Employee Id : 101
Enter Employee Name : mohamad
Enter Employee Age : 25
Enter Employee Salary : 10000
Enter details of 2 Employee
Enter Employee Id : 102
Enter Employee Name : nour
Enter Employee Age : 32
Enter Employee Salary : 2000
Enter details of 3 Employee
Enter Employee Id : 103
Enter Employee Name : laila
Enter Employee Age : 28
Enter Employee Salary : 3000
Enter details of 4 Employee
Enter Employee Id : 104
Enter Employee Name : joud
Enter Employee Age : 39
Enter Employee Salary : 40000
Enter details of 5 Employee
Enter Employee Id : 105
Enter Employee Name : reem
Enter Employee Age : 23
Enter Employee Salary : 5000
------------------------------------------------------------
Details of Employees
------------------------------------------------------------
ID Name Age Salary
101 mohamad 25 10000
102 nour 32 2000
103 laila 28 3000
104 joud 39 40000
105 reem 23 5000
------------------------------------------------------------
need an explanation for this answer? contact us directly to get an explanation for this answer