I am again working on example from book and something does not add-up literally ;) i think.... I don't know imperial units to well but as far as I understand
in 1 feet there is 12 inches.
// ReturningStructureVariables.cpp : Defines the entry point for the console application.
//
#include <iostream>
struct distance
{
int feet;
double inches;
};
distance addEngl(distance, distance);
void englDisp(distance);
int main()
{
distance d1, d2, d3; // define 3 lenghts
//get lenght from user
std::cout << "1st user Enter feet: ";
std::cin >> d1.feet;
std::cout << "1st user Enter inches: ";
std::cin >> d1.inches;
//same for 2nd user
std::cout << "2nd user Enter feet: ";
std::cin >> d2.feet;
std::cout << "2nd user Enter inches: ";
std::cin >> d2.inches;
d3 = addEngl(d1, d2); //d3 is sum of d1 and d2
std::cout << std::endl;
englDisp(d1); //display all lenghts
std::cout << " + ";
englDisp(d2);
std::cout << " = ";
englDisp(d3);
std::cout << std::endl;
system("pause");
return 0;
}
// addEngl()
// adds two structures of tyep Distance, returns sum
distance addEngl(distance dd1, distance dd2)
{
distance dd3; // define a new structure for sum
dd3.inches = dd1.inches + dd2.inches; // add the inches
dd3.feet = 0; // for possible carry (i think if inches go over size 12 then it will be converted to 1 feet for each 12 inches
if (dd3.inches >= 12.0) //if inches >= 12.0,
{
dd3.inches -= 12.0; // decrease inches by 12.0 and increase
dd3.feet++; // feet by 1
}
dd3.feet += dd1.feet + dd2.feet; // add the feet dd3.feet = dd3.fett + dd1.feet + dd2.feet;
return dd3;
}
//englDisp()
// display structures of type Distance in feet and inches
void englDisp(distance dd)
{
std::cout << dd.feet << "\' - " << dd.inches << "\" ";
}
however, when I add 23 feet plus 12 inches and for second user 23 feet and 13 inches I am getting result
47 Feet and 13 inches. Should that be 48 Feet and 1 inch????
google can convert for you but really fast, since I was born using imperial...
12 inches is 1 foot. therefore 23 feet + 12 inches = 24 feet.
also, 1 inch is 2.54 cm exact, if you need to go metric later.
a yard is 3 feet, may also be useful to know.
23 feet + 13 inches is 24+1
the sum of 23 . 12 and 23.13 is 48.1 .. as above, its 24 feet for the first user, 24 and 1 inch for 2nd, and the sum of THOSE is 48 &1
Oh, and code-wise, the easy thing to do is convert all data to inches, store only inches, and only deal with feet when you do output.
so convert 23 feet to 23*12 inches and store THAT + whatever left over inches. Its just easier to code the class this way, IMHO.
Then when printing, just do inches%12 is feet and inches/12 (integer) to get the values.
Finally, be aware that imperial users want to deal with inches in binary (powers of 2) fractions. It is common to talk about 1/2, 1/4, ... 1/32 inches. Beyond 1/32 is rare.
Jlb - nope its if statement only, if you have access to the book Object Orientated Programming in C++ 4th Edition. I was thinking about using a loop to get correct result but Jonning suggestion I think solve this problem in a neat way. Plus my lack of proper understanding of the imperial units does not help as well :D
Jonnin I know google converts but only if to one value so I will get result in inches or in feet with floating point