Posted by: Sourav | February 16, 2013

Returning a structure using function in C++


#include <iostream>
using namespace std;

struct Distance
{
int feet;
float inches;
};

 Distance addition (Distance,Distance);
void showstruct(Distance);

int main ()
{
  Distance d1,d2,d3;
cout<<“Enter the feet”<<endl;
cin>>d1.feet;
cout<<“Enter the inches”<<endl;
cin>>d1.inches;
cout<<“Enter the feet”<<endl;
cin>>d2.feet;
cout<<“Enter the inches”<<endl;
cin>>d2.inches;

d3=addition(d1,d2);

showstruct(d1);
cout<<” + “;
showstruct(d2);
//cout<<endl;
cout<<” = “<<d3.feet<<” feet “<<d3.inches<<” inches”<<endl;

  return 0;
}

void showstruct(struct Distance dd)
{
cout<<dd.feet<<” feet “<<dd.inches<<” inches “;

}

Distance addition (Distance dd1,Distance dd2)
{

Distance d4;
d4.feet=0;
d4.inches=dd1.inches+dd2.inches;

if(d4.inches>=12.0)
{
d4.feet+=1;
d4.inches-=12.0;
}
d4.feet+=dd1.feet+dd2.feet;
return d4;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: