Below is a code I wrote to convert user input of inches to miles, yards, feet. My only issue is that the format in which I would like this to be output would require the output to have "0 inches" as well.
For the life of me I cannot figure that much out. I tried setting a new int value to inches and have it return 0, but that just confused things even more.
Thanks for any help.
#include <iostream>
using namespace std;
int
main ()
{
double m;
double y;
double f;
double i;
cout << " Enter the Length in inches:";
cin >> i;
m = i / 63360; // Convert To Miles
y = 1760 * m; // Convert To Yards
f = 3 * y; // Convert to Feet
i = 12 * f; // Convert to Inches
cout << i << "inches =" << " " << m << " " << "(mile)s," << " " <<
y << " " << "yards," << " " << f << " " << "feet," << " " << i <<
" " << "inches." << endl;
return 0;
My guess is you want the calculation to cascade such as it takes inches and roles it up to the biggest units possible. So 15" would go to 1' 3". If my guess is wrong ignore this answer.
#include <iostream>
using namespace std;
static const int INCHES_PER_MILE = 63360;
static const int INCHES_PER_YARD = 36;
static const int INCHES_PER_FOOT = 12;
int main ()
{
int inches, m, y, f ,i, remainder; //int so that we dont get decimal values
cout << " Enter the Length in inches:";
cin >> inches;
m = inches / INCHES_PER_MILE ; // Convert To Miles -- shouldn't be 'magic numbers'
remainder= inches % INCHES_PER_MILE ; // % stands for modulo -- (i.e. take the remainder)
y = remainder / INCHES_PER_YARD; // Convert To Yards
remainder = remainder % INCHES_PER_YARD;
f = remainder / INCHES_PER_FOOT; // Convert to Feet
remainder = remainder % INCHES_PER_FOOT;
i = remainder; // Convert to Inches
cout << inches << " inches = " << m <<" (mile)s, " <<
y << " yards, " << f << " feet, " << i << " inches." << endl;
return 0;
}