Structure

 Here We are going to make a structure variable in this lesson. Here we will be creating a structure named 's' and we will be taking information about name, age and height. 

 /*Structure*/

#include<iostream>

using namespace std;

struct student{char name[20];

int age;

float height;};

int main(){

struct student s;

cout<<"Enter name:"; cin>>s.name;

cout<<"Enter age:"; cin>>s.age;

cout<<"Enter height:"; cin>>s.height;

cout<<"You have entered following data:"<<endl;

cout<<"Your Name:"<<s.name<<endl;

cout<<"Your age:"<<s.age<<endl;

cout<<"Your height:"<<s.height;

return 0;}


Here We will do the same activity using function.

/*Structure*/
#include<iostream>
using namespace std;
struct student{char name[20];
int age;
float height;};
student s; //It's not important to write struct in C++

void input(){
cout<<"Enter name:"; cin>>s.name;
cout<<"Enter age:"; cin>>s.age;
cout<<"Enter height:"; cin>>s.height;
}
void output(){
cout<<"You have entered following data:"<<endl;
cout<<"Your Name:"<<s.name<<endl;
cout<<"Your age:"<<s.age<<endl;
cout<<"Your height:"<<s.height;
}

int main(){
input();
output();


return 0;}


This Can be Written in this way as well. Which C program Does not support. Here you can see in this Program we had written out code inside the Struct of Student and input and output are functioned and later it is called on int main. Where we had prepared a structure named 's' and it is called with function of s.input() and s.output().

/*Structure in C++*/
#include<iostream>
using namespace std;
struct student{
char name[20];
int age;
float height;

void input(){
cout<<"Enter name:"; cin>>name;
cout<<"Enter age:"; cin>>age;
cout<<"Enter height:"; cin>>height;
}

void output(){
cout<<"You have entered following data:"<<endl;
cout<<"Your Name:"<<name<<endl;
cout<<"Your age:"<<age<<endl;
cout<<"Your height:"<<height;
}
};

int main(){
student s;
s.input();
s.output();


return 0;}


0 Comments had been done yet:

Post a Comment