C++ static_cast Converts a length in yards as a decimal value to yards, feet, and inches.

Description

C++ static_cast Converts a length in yards as a decimal value to yards, feet, and inches.

#include <iostream>

int main()/*from www  . j ava2 s.c o  m*/
{
  const unsigned int feet_per_yard {3};
  const unsigned inches_per_foot {12};

  double length {};                    // Length as decimal yards
  unsigned int yards{};                // Whole yards
  unsigned int feet {};                // Whole feet
  unsigned int inches {};              // Whole inches

  std::cout << "Enter a length in yards as a decimal:  ";
  std::cin >> length;

  // Get the length as yards, feet, and inches
  yards = static_cast<unsigned int>(length);
  feet = static_cast<unsigned int>((length - yards)*feet_per_yard);
  inches = static_cast<unsigned int>
                       (length*feet_per_yard *inches_per_foot) % inches_per_foot;

  std::cout << length << " yards converts to "
            << yards  << " yards "
            << feet   << " feet "
            << inches << " inches." << std:: endl;
}



PreviousNext

Related