C++ Program to assign data to members of a structure variable and display it.
#include<iostream>
using namespace std;
struct proff
{
char name[50];
int age;
float salary;
};
int main()
{
Proff p;
cout << "Enter Full name: ";
cin.get(p.name, 50);
cout << "Enter age: ";
cin >> p.age;
cout << "Enter salary: ";
cin >> p.salary;
cout << "\nDisplaying Information." << endl;
cout << "Name: " << p.name << endl;
cout <<"Age: " << p.age << endl;
cout << "Salary: " << p.salary;
return 0;
}
C++ Program to Store Information of a Student in a Structure.
#include<iostream>
using namespace std;
struct student
{
char name[50];
int roll;
float marks;
};
int main()
{
student s;
cout << "Enter information," << endl;
cout << "Enter name: ";
cin >> s.name;
cout << "Enter roll number: ";
cin >> s.roll;
cout << "Enter marks: ";
cin >> s.marks;
cout << "\nDisplaying Information," << endl;
cout << "Name: " << s.name << endl;
cout << "Roll: " << s.roll << endl;
cout << "Marks: " << s.marks << endl;
return 0;
}
C++ Program to Store and Display Information of Employees Using Structure
#include<iostream>
using namespace std;
struct employee
{
char ename[50];
int eid;
float salary;
} e[10];
int main()
{
cout << "Enter information of employees: " << endl;
// storing information
for(int i = 0; i < 10; ++i)
{
e[i].eid = i+1;
cout << "For roll number" << e[i].eid << "," << endl;
cout << "Enter name: ";
cin >> e[i].ename;
cout << "Enter marks: ";
cin >> e[i].salary;
cout << endl;
}
cout << "Displaying Information: " << endl;
// Displaying information
for(int i = 0; i < 10; ++i)
{
cout << "\nEmployee id is: " << i+1 << endl;
cout << " Employee Name: " << e[i].ename << endl;
cout << "Employee salary: " << e[i].salary << endl;
}
return 0;
}
C++ Program To Add Two Distances (in inch-feet) Using Structures
#include<iostream>
using namespace std;
struct Distance {
int feet;
float inch;
}d1 , d2, sum;
int main() {
cout << "Enter 1st distance," << endl;
cout << "Enter feet: ";
cin >> d1.feet;
cout << "Enter inch: ";
cin >> d1.inch;
cout << "\nEnter information for 2nd distance" << endl;
cout << "Enter feet: ";
cin >> d2.feet;
cout << "Enter inch: ";
cin >> d2.inch;
sum.feet = d1.feet+d2.feet;
sum.inch = d1.inch+d2.inch;
// changing to feet if inch is greater than 12
if(sum.inch > 12) {
int extra = sum.inch / 12;
sum.feet += extra;
sum.inch -= (extra * 12);
}
cout << endl << "Sum of distances = " << sum.feet << " feet " << sum.inch << " inches";
return 0;
}