0

I am writing a program that executes two operations on a structure distanceType which is compose of three integer members: feet, yards, and miles.The function convertYards takes a distance in yards that is greater than or equal to 1760 (because there are 1760 yards in a mile) and converts it to x miles, y yards, and z feet (i.e. distanceType). I'm having trouble with the function parameters. convertYards takes in one integer value and returns a distanceType. The problem is I am not sure how to define the function. Since it can accept either distance1 or distance2 (integer values), a small part of the code is below, the area with the "???" is what I am confused about.

struct distanceType
{
  int miles;
  int yards;
  int feet;
}

distanceType convertYards(int ???)  //Define convertYards

Any help is appreciated, thanks.

7
  • 1
    The entire idea is insane. You should store distance as a single quantity, in whichever unit of your choosing, and then format it into any desired representation at the time of printing it. Commented Nov 13, 2012 at 20:24
  • 1
    Personally, I like the idea of having separate Yard, Mile, etc. classes that have an implicit conversion operator to one base unit that you use for functions. It's easy to combine that with C++11's user-defined literals to call foo(5_miles) or foo(20_yards). Commented Nov 13, 2012 at 20:24
  • What is distance1 and distance2? Is this function, convertYards() supposed to take a number representing a distance which may be in more than one different unit? For instance the integer may represent the number of yards or the integer may represent the number of miles? Commented Nov 13, 2012 at 20:27
  • I am not sure what this means, I am a beginner with programming, can you please clarify? And I have two distance variables, distance1 and distance2, because a user can enter two different distances, so I might have to convert both using the function. Commented Nov 13, 2012 at 20:29
  • @RichardChambers Say a user enters 5230 yards. Then the function will convert it to 0 feet, 1710 yards, and 2 miles. Commented Nov 13, 2012 at 20:30

1 Answer 1

1

So something like the following code which has not been compiled so may need some changes but should capture the essence.

struct distanceType
{
  int miles;
  int yards;
  int feet;
}

distanceType convertYards(int nYards)
{
    distanceType distance;

    distance.miles = nYards / 1760;
    distance.yards = nYards - distance.miles * 1760;
    distance.feet = 0;   // since value is integer number of yards, feet is always zero
    return distance;
}

This this function would be used as follows:

int main ()
{
   distanceType dt1, dt2;
   int          y1 = 3500, y2 = 5700;

   dt1 = convertYards (y1);
   dt2 = convertYards (y2);

   printf (" %d yards = %d miles, %d yards, and %d feet", y1, dt1.miles, dt1.yards, dt1.feet);
   printf (" %d yards = %d miles, %d yards, and %d feet", y2, dt2.miles, dt2.yards, dt2.feet);
   return 0;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Shouldn't feet always be nYards*3 since there are 3 feet per yard?
@ThomasMatthews, It's doing as many miles as it can, followed by as many leftover yards as it can, followed by leftover feet. Since the yards are always integral, there is no need for feet.
If I enter 1 yard, there should be 3 feet, but the miles will be zero because its integral. Also, I can enter -27 for yards.
@ThomasMatthews If you enter 1 yard, then it will stay 1 yard, it won't convert to feet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.