Showing posts with label Structures. Show all posts
Showing posts with label Structures. Show all posts

C++ program to illustrate passing of structures by value.

Sunday, August 12, 2012


Write a C++ program to illustrate passing of structures by value. 

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct distance{
 int feet;
 int inches;
};
int main()
{
clrscr();
    distance length1, length2;
void prnsum(distance l1, distance l2);
cout<<"\n"<<"Enter length 1:\n";
cout<<"\n"<<"Feet:\n";
cin>>length1.feet;
cout<<"\n"<<"Inches:\n";
cin>>length1.inches;
cout<<"\n"<<"\n"<<"Enter length 2:\n";
cout<<"\n"<<"Feet:\n";
cin>>length2.feet;
cout<<"\n"<<"Inches\n";
cin>>length2.inches;
prnsum(length1, length2);
return 0;
}
void prnsum(distance  l1, distance l2)
{
  distance l3;
  l3.feet = l1.feet + l2.feet + (l1.inches +l2.inches)/12;
  l3.inches =( l1.inches + l2.inches)%12;
  cout<<"\n"<<"\n";
  cout<<"\n"<<"Total Feet:"<<l3.feet<<"\n";
  cout<<"\n"<<"Total Inches:"<<l3.inches;
  return;
}
/*
OUTPUT
------

Enter length 1:

Feet:
10

Inches:
1

Enter length 2:

Feet:
12

Inches
2

Total Feet:22

Total Inches:3


*/

C++ Program

Wednesday, August 1, 2012


Write a C++ Program to store information of 10 employees and to display information of an 
employee depending upon the employee no.


#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct addr
{
int houseno;
char area[30];
char city[30];
char state[30];
};
struct emp
{
int empno;
char name[30];
char desig[30];
addr address;
float basic;
}worker;
emp sales_emp[10];
void display(int a);
void enter(void);
int main()
{
clrscr();
int eno,i;
char ch;
enter();
do
{
cout<<"\n Enter the employee no. whose information is to be displayed:";
cin>>eno;
int flag=0;
for(i=0;i<2;i++)
{
if(sales_emp[i].empno==eno)
{
display(i);
flag=1;
break;
}
}
if(!flag)
{
cout<<"\n sorry! no such employee found";
}
cout<<"\n display more...(y/n)\n";
cin>>ch;
}while(ch=='y');
getch();
return 0;
}
void enter(void)
{
for(int i=0;i<2;i++)
{
cout<<"\n employee no:";
cin>>sales_emp[i].empno;
cout<<"\n name:";
gets(sales_emp[i].name);
cout<<"\n designation:";
gets(sales_emp[i].desig);
cout<<"\n * address *";
cout<<"\n house no:";
cin>>sales_emp[i].address.houseno;
cout<<"\n area:";
gets(sales_emp[i].address.area);
cout<<"\n city:";
gets(sales_emp[i].address.city);
cout<<"\n state:";
gets(sales_emp[i].address.state);
cout<<"\n basic pay:";
cin>>sales_emp[i].basic;
cout<<"\n\n";
}
return;
}
void display(int a)
{
cout<<"\n employee data";
cout<<"\n enployee no.:"<<sales_emp[a].empno;
cout<<"\n name:";
cout.write(sales_emp[a].name,30);
cout<<"\n designation:";
cout.write(sales_emp[a].desig,30);
cout<<"\n address:"<<sales_emp[a].address.houseno;
cout<<" \n";
cout.write(sales_emp[a].address.area,30);
cout<<" \n";
cout.write(sales_emp[a].address.city,30);
cout<<" \n";
cout.write(sales_emp[a].address.state,30);
cout<<" \n";
cout<<"\n basic pay:"<<sales_emp[a].basic;
cout<<"\n\n";
return;
}

Blogroll

Bookmark and Share
Powered By Blogger
CBSE 12th Grader.