Q:

C++ Program To Add Two Distances (in inch-feet) Using Structures

belongs to collection: Examples of Structure C++ Programming

0

Defining a structure
struct keyword is used to define a structure. struct define a new data type which is a collection of different type of data.



Syntax :

struct structure_name
{
//Statements
};

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <iostream>

using namespace std;

struct Distance
{
int feet;
float inch;
}d1,d2,sum;

int main()
{

cout << "Enter information for 1st distance" << endl;
cout << "Enter feet: ";

cin >> d1.feet;

cout << "Enter inch: ";
cin >> d1.inch;

cout << endl << "Enter 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;

do
{
sum.inch=sum.inch-12.0;
++sum.feet;
}while(sum.inch>12.0);

cout << endl << "Sum of distances= " << sum.feet << " feet " << sum.inch << " inch";

return 0;

}

 

Output:

Enter information for 1st distance

Enter feet: 23

Enter inch: 6

Enter information for 2nd distance

Enter feet: 52

Enter inch: 8

Sum of distances= 76 feet 2 inch

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Program For Employee Information Using Nested ... >>
<< C++ Program For Store Employee Information And Dis...