In this program, we store information about student using structure. This program contains a structure (student), using this structure you are able to store student name, roll number and course. Actually, student is user-defined data type.
C++ Structure Program to Store Information of a Student
PROGRAM:
#include<iostream>
#include<stdio.h>
using namespace std;
struct student
{
char name[20];
int rollno;
char course[20];
};
int main()
{
student s1;
cout<<endl;
cout<<"Enter the detail of student"<<endl;
cout<<"Enter the name of student : ";
cin.get(s1.name,20);
cout<<"Enter roll number of student : ";
cin>>s1.rollno;
cout<<"Enter course : "; fflush(stdin);
cin.get(s1.course,20);
cout<<endl<<"Name of student : "<<s1.name;
cout<<endl<<"Roll number : "<<s1.rollno;
cout<<endl<<"Course : "<<s1.course;
return 0;
}
void display(student s)
{
cout<<endl<<"Name of student : "<<s.name;
cout<<endl<<"Roll number : "<<s.rollno;
cout<<endl<<"Course : "<<s.course;
}
Output:
Enter the detail of student
Enter the name of student : Bharat Chaturvedi
Enter roll number of student : 10
Enter course : C++ Language
Name of student : Bharat Chaturvedi
Roll number : 10
Course : C++ Language
In this program, we use function (display) to print student information by taking structure as parameter.
Another Structure Program in C++ to Store Information of Student
#include<iostream>
#include<stdio.h>
using namespace std;
struct student;
student getdata(student s);
void display(student s);
struct student
{
char name[20];
int rollno;
char course[20];
};
int main()
{
student s1;
s1=getdata(s1);
display(s1);
return 0;
}
student getdata(student s)
{
cout<<endl;
cout<<"Enter the detail of student"<<endl;
cout<<"Enter the name of student : ";
cin.get(s.name,20);
cout<<"Enter roll number of student : ";
cin>>s.rollno;
cout<<"Enter course : "; fflush(stdin);
cin.get(s.course,20);
return s;
}
void display(student s)
{
cout<<endl<<"Name of student : "<<s.name;
cout<<endl<<"Roll number : "<<s.rollno;
cout<<endl<<"Course : "<<s.course;
}
Output:
Enter the detail of student
Enter the name of student : Bharat Chaturvedi
Enter roll number of student : 10
Enter course : C++ Language
Name of student : Bharat Chaturvedi
Roll number : 10
Course : C++ Language
In this program, we use function (getdata) for receiving student information and function (display) to print student information. getdata function takes structure as argument and after storing information of student, it returns structure to main function.
0 comments:
Post a Comment