Structure is a collection of variable of different datatypes. It is declare by following syntax.
Syntax :-
struct student {
int Rno , age; // Variable declare with int data type Rno & age
char sname[20]; // Array declare with char data type sname[20]
}
Note:-
student name[20]; //Array of structure.
gets(name[i].sname); //Taking String from user gets() function is used.
puts(name[j].sname); //For cout or Output string puts() function is used. To use this function use header file #include<stdio.h>.
Example
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
struct student { int Rno, age;
char sname[20];
};
student name[20];
for(int i=0; i<20; i++) // Loop for input the data
{
cout<<"Enter the roll No."<<endl;
cin>>name[i].Rno; //Input the Name from the user
cout<<"Enter the age"<<endl;
cin>>name[i].age; //Input the Age from the user
cout<<"Enter the Name."<<endl;
gets(name[i].sname); //Input the Name form the User
}
for(int j=0; j<20; j++) //Loop for output the data
{
cout<<"\n"<<"\t"<<"Information"<<"\n"<<endl;
cout<<"Rno =" <<name[j].Rno<<"\n"<<endl; // Output the Rno
cout<<"Age ="<<name[j].age<<"\n"<<endl; // Output the Age
cout<<"Name =";
puts(name[j].sname); // Output the Name
cout<<"\n"<<endl;
}
getch();
}
Output:-
0 Comments