In this program, you'll learn to declare structure 'distance' having member variables are km and m. Accept the two distances and you will get the total distance.
#include<iostream>
using namespace std;
struct distance
{
int km, m;
};
int main()
{
struct distance d1, d2, d3;
cout<<"Enter first distance KM and M: ";
cin>> d1.km >> d1.m;
cout<<"Enter second distance KM and M: ";
cin>> d2.km >> d2.m;
d3.m = d1.m + d2.m;
d3.km = d1.km + d2.km;
if(d3.m >= 1000)
{
d3.m = d3.m - 1000;
d3.km++;
}
cout<<"\nTotal distance is: " << d3.km << " KM and " << d3.m << " meters" ;
return 0;
}
Enter first distance KM and M: 5 750
Enter second distance KM and M: 12 500
Total distance is: 18 KM and 250 meters