C++ Function return statement returning a structure

Description

C++ Function return statement returning a structure

#include <iostream>
using namespace std;
struct Measure/*from  ww  w  . j  av  a 2  s. c o m*/
{
   int feet;
   float inches;
};
Measure addengl(Measure, Measure);  //declarations
void engldisp(Measure);
int main()
{
   Measure d1, d2, d3;                //define three lengths
   //get length d1 from user
   cout << "\nEnter feet: ";  cin >> d1.feet;
   cout << "Enter inches: ";  cin >> d1.inches;
   //get length d2 from user
   cout << "\nEnter feet: ";  cin >> d2.feet;
   cout << "Enter inches: ";  cin >> d2.inches;
   d3 = addengl(d1, d2);               //d3 is sum of d1 and d2
   cout << endl;
   engldisp(d1); cout << " + ";        //display all lengths
   engldisp(d2); cout << " = ";
   engldisp(d3); cout << endl;
   return 0;
}
// adds two structures of type Measure, returns sum
Measure addengl( Measure dd1, Measure dd2 )
{
   Measure dd3;                //define a new structure for sum
   dd3.inches = dd1.inches + dd2.inches;  //add the inches
   dd3.feet = 0;                       //(for possible carry)
   if(dd3.inches >= 12.0)              //if inches >= 12.0,
   {                                //then decrease inches
       dd3.inches -= 12.0;              //by 12.0 and
       dd3.feet++;                      //increase feet
   }                                //by 1
   dd3.feet += dd1.feet + dd2.feet;    //add the feet
   return dd3;                         //return structure
}
// display structure of type Measure in feet and inches
void engldisp( Measure dd )
{
    cout << dd.feet << "\'-" << dd.inches << "\"";
}



PreviousNext

Related