by Dinesh Thakur Category: Array Pointer and Union

Structure elements can be referenced by writing the structure variable before the structure element. The structure variable and the structure element are separated by a dot (.) which is called the dot operator. For example

 

st.name

st is a structure variable and name is an element in the structure

 

The program illustrates how to input and output elements in a structure. Details of employees namely code, name and salary are given as input in the form of a structure. The details of employees are also displayed in the same program.

 

#include<iostream.h>

void main()

{

char ch;

struct employee

{

int code;

char name[10];

float salary;

} emp;

do

{

cout <<"Give the code of the employee: ";

cin >> emp.code;

cout<< "Give the name of employee: ";

cin >> emp.name;

cout<< "Give the salary of the employee: ";

cin >> emp.salary;

cout<<"\nCode: "<<emp.code<<'\n';

cout<<"Name: "<<emp.name<<'\n';

cout<<"Salary: "<<emp.salary<<'\n';

cout<<"\nDo you want to enter more data? Type y or n. ";

cin>>ch;

} while (ch= 'y');

}

 

Input and output:

Give the code of the employee: 10

Give the name of the employee: Sharma

Give the salary of the employee: 10000

Code: 10

Name: Sharma

Salary : 10000

Do you want to enter more data? Type y or n. y

Give the code of the employee: 20

Give the name of the employee: Jyothi

Give the salary of the employee: 20000

Code: 20

Name: Jyothi

Salary : 20000

Do you want to enter more data? Type y or n. n



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles