Q:

C++ Program For Store Employee Information And Display Using Structure

belongs to collection: Examples of Structure C++ Programming

0
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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include<iostream>
#include<iomanip>
using namespace std;

struct Employee
{
    int Id;
    char Name[25];
    int Age;
    long Salary;
};

int main()
{
    int i, n;  

 
 cout<<"\nEnter The Number of Employee\n\n";
 cin>>n;
 
 Employee Emp[n]; // Structure object created emp
 
    for(i=0;i<n;i++)
    {

    cout << "\nEnter details of " << i+1 << " Employee"<<endl;

    cout <<setw(5)<< "\nEnter Employee Id : ";
    cin >> Emp[i].Id;

    cout <<setw(5)<< "\nEnter Employee Name : ";
    cin >> Emp[i].Name;

    cout <<setw(5)<< "\nEnter Employee Age : ";
    cin >> Emp[i].Age;

    cout <<setw(5)<< "\nEnter Employee Salary : ";
    cin >> Emp[i].Salary;
    
 }

 cout<<"\n\n------------------------------------------------------------\n";
    cout << "Details of Employees";
 cout<<"\n------------------------------------------------------------\n\n";
    
    cout << "ID" <<setw(15)<<"Name" <<setw(10)<<"Age" <<setw(10)<<"Salary";
    cout<<endl;
 for(i=0;i<n;i++)
    {
     
     cout << "\n"<< Emp[i].Id <<setw(15)<< Emp[i].Name <<setw(10)<< Emp[i].Age <<setw(10)<< Emp[i].Salary;
    }
    cout<<"\n\n------------------------------------------------------------\n";
 
}

 

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

total answers (1)

C++ Program To Add Two Distances (in inch-feet) Us... >>