fastread homefastrread library fastread menu

C++ : Add Two Distances (in inch-feet) System Using Structures

Tutorial by:Manisha Dubey      Date: 2016-06-18 05:15:16

❰ Previous Next ❱

This program takes two distances in inch-feet system and stores in data members of two structure variables. Then, this program calculates the sum of two distances and displays it.

Source code to add two distance using structure

#include <iostream>

using namespace std;

struct Distance{
    int feet;
    float inch;

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;

/* If inch is greater than 12, changing it to feet. */

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


Enter information for 1st distance
Enter feet: 12
Enter inch: 3.45

Enter information for 1st distance
Enter feet: 12
Enter inch: 9.2

Sum of distances= 25 feet  0.65 inch

 In this program, a structure Distance is defined with inch and feet as its members. Then, three variables(d1, d2 and sum) of struct Distance type is created. Two variables(d1 and d2) are used for taking distance from user and the sum of two distance is stored in variable sum and then, displayed.

❰ Previous Next ❱


Submit Your Thought, Tutorial, Articls etc.

Submit Your Information India's Number one online promotion website