Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Structure

 How to pass structures to functions?


 How to use pointers with structures?

Structure eg

#include <iostream>

using namespace std;

struct person

char name[50];

int age;

float salary;

};

int main()

person p1;

cout<<"Enter name:";

cin>>p1.name;

cout<<"Enter age:";

cin>>p1.age;

cout<<"Enter salary: ";

cin>>p1.salary;

cout<<"****Details of employees****"<< endl;

cout<<"Name:"<< p1.name<< endl;

cout<<"age:"<< p1.age<< endl;


cout<<"salary :" << p1.salary;

return 0;

Switch eg

#include <iostream>

using namespace std;

int main()

int choice;

cout<<"enter a choice 1,2, 3 : ";

cin>>choice;

switch(choice)

case 1:

cout<<"you selected choice 1";

break;

case 2:

cout<<"you selected choice 2";

break;

case 3:

cout<<"you selected choice 3";

break;

default:
cout<<"you selected wrong choice ";

break;

return 0;

If else eg

#include <iostream>

using namespace std;

int main()

int choice;

cout<<"enter a choice 1,2,3 : ";

cin>>choice;

if(choice==1)

cout<<"you selected choice 1";

else if(choice ==2)

cout<<"you selected choice 2";

else if(choice ==3)

cout<<"you selected choice 3";


}

else

cout<<"you selected wrong choice ";

return 0;

#include <iostream>

using namespace std;

struct person

char name[50];

int age;

float salary;

};

int main()

person p1, p2;

cout<<"Enter name:";

cin>>p1.name;

cout<<"Enter age:";

cin>>p1.age;

cout<<"Enter salary: ";


cin>>p1.salary;

cout<<"Enter name of 2nd emp:";

cin>>p2.name;

cout<<"Enter ageof 2nd emp:";

cin>>p2.age;

cout<<"Enter salary of 2nd emp: ";

cin>>p2.salary;

cout<<"\n ****Details of 1st employees****"<< endl;

cout<<"Name:"<< p1.name<< endl;

cout<<"age:"<< p1.age<< endl;

cout<<"salary :" << p1.salary;

cout<<"****\n Details of 2nd employees****"<< endl;

cout<<"Name:"<< p2.name<< endl;

cout<<"age:"<< p2.age<< endl;

cout<<"salary :" << p2.salary;

return 0;

You might also like