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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #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