0

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;
  • 2
    The question is really unclear. Tell me one thing, you have inches from user input then why are you calculating it again? – Chandan Purbia Jul 21 '17 at 19:24
  • It is for a project, and the specifics for output are as follows, Enter the number of inches: 12300000 12300000 inches = 194 miles, 226 yards, 2 ft, 0 in. – Robert Nelson Jul 21 '17 at 19:36
  • i may be taking things very literal, but I thought I would ask better minds than me to see if there was a way to have the output mirror that. – Robert Nelson Jul 21 '17 at 19:37
  • Your code isn't calculating anything close to your desired output. Think about how you're figuring out this answer by hand, then duplicate that method in code. – Mark Ransom Jul 21 '17 at 19:48

2 Answers2

2

This is probably closer to what you want:

// ...

int m;
int y;
int f;
int i;
int len;

cout << " Enter the Length in inches:";
cin >> len;

cout << len << "inches = ";

m = len / 63360;        // Miles
len -= m * 63360;
y = len / 36;           // Yards
len -= y * 36;
f = len / 12;           // Feet
i = len % 12;           // Inches

if (m)
    cout << m << " (mile)s, ";
if (y)
    cout << y << " yards, "; 
if (f)
    cout << f << " feet, ";

cout << i << " inches." << endl;

// ...
Michaël Roy
  • 6,338
  • 1
  • 15
  • 19
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;
}
drescherjm
  • 10,365
  • 5
  • 44
  • 64