/* Lengths.c */
#include <stdio.h>
#include <string.h>

main()
{
 double length_in_feet, converted_length, factor;
 int choice;
 char units[11];

 /* Ask the user for the length to be converted */
 printf("What is the length in feet? ");
 scanf("%lf", &length_in_feet);

 /* Display the menu of conversion options */
 printf("\nTo what units would you like to convert the length?\n");
 printf("1 - Inches\n");
 printf("2 - Meters\n");
 printf("3 - Yards\n");
 printf("4 - Kilometers\n");
 printf("Enter the number of the unit you want: ");
 scanf("%d", &choice);

 /* Use a switch structure to set the conversion factor */
 switch(choice)
  {
    case 1: /* inches */
      factor = 12.0;
      strcpy(units,"inches");
      break;
    case 2: /* meters */
      factor = 0.30479999;
      strcpy(units,"meters");
      break;
    case 3: /* yards */
      factor = 0.33333333;
      strcpy(units,"yards");
      break;
    case 4: /* kilometers */
      factor = 0.00030479;
      strcpy(units,"kilometers");
      break;
    default:
      factor = 0;
      break;
  }

 if (factor == 0) /* check for illegal selection */
   {
     printf("Illegal selection--no result calculated\n");
   }
 else /* if not illegal, do the conversion and print the output */
   {
     converted_length = length_in_feet * factor;
     printf( "%.3lf feet = %.3lf %s\n", length_in_feet, converted_length, units);
   }

 return 0;
}
