{"id":4482,"date":"2020-12-01T00:01:38","date_gmt":"2020-12-01T08:01:38","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4482"},"modified":"2020-12-08T08:46:31","modified_gmt":"2020-12-08T16:46:31","slug":"some-weighty-conversions","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4482","title":{"rendered":"Some Weighty Conversions"},"content":{"rendered":"<p>The three units of weight I&#8217;m familiar with are pounds, kilograms, and stone. For me, stone is the odd one. I&#8217;ve heard it used in Britain and it&#8217;s completely baffling to me, often requiring that I whip out a calculator to see what &#8220;14 stone&#8221; really means. Better, I could write code that converts between pounds, kilograms, and stone, which is the goal of this month&#8217;s Exercise.<br \/>\n<!--more--><br \/>\nFor example, if I hear &#8220;14 stone&#8221; on a British TV show, I must do the math in my head: One stone is 14 pounds, so 14 stone is 14 &times; 14, which is (uses calculator) 196 pounds.<\/p>\n<p>To make such a conversion in a computer program, you could input information like this:<\/p>\n<p><code>14SP<\/code><\/p>\n<p>This cryptic text reads, &#8220;Convert 14 stone into pounds.&#8221; The value <code>14<\/code> is the quantity, and the single-letter suffixes control the conversion: <code>S<\/code> for stone into <code>P<\/code> for pounds.<\/p>\n<p>Or if you wanted to know how many kilograms are in 196 pounds, you type:<\/p>\n<p><code>196PK<\/code><\/p>\n<p>Which translates as, &#8220;Convert 196 pounds into kilograms.&#8221; The computer dutifully spits out the value 88.9 kilos.<\/p>\n<p>Your task for this month&#8217;s Exercise is to write such a program.<\/p>\n<p>The code prompts for input, as in this example:<\/p>\n<p><code>Type a value to convert, suffixed by conversion types:<br \/>\nK - kilos, P - pounds, S - stones<br \/>\nExample: 2.0PK (convert 2.0 pounds to kilograms)<br \/>\nConvert:<\/code><\/p>\n<p>The text input (standard input) is examined to pluck out a value from the string. Then the code examines the next two characters to determine the conversion. Invalid input is rejected.<\/p>\n<p>The conversions required are:<\/p>\n<p>Kilos to Pounds: 2.20462<br \/>\nKilos to Stone: 0.127473<br \/>\nPounds to Kilos: 0.453592<br \/>\nPounds to Stone: 0.714286<br \/>\nStone to Kilos: 6.35029<br \/>\nStone to Pounds: 14.0<\/p>\n<p>This Exercise began as a simple conversion table, but I decided to spice it up and have the code determine the conversion based upon input. This addition is the real challenge, not only to properly convert the string input into a value but to validate and interpret the two conversion characters.<\/p>\n<p>Please try this Exercise on your own before you peek to see <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4490\">my solution<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The computer masterfully converts weight measurements, providing you supply an initial value and tell it which measurements to convert to and from. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4482\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-4482","post","type-post","status-publish","format-standard","hentry","category-exercise"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4482","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4482"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4482\/revisions"}],"predecessor-version":[{"id":4524,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4482\/revisions\/4524"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4482"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4482"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4482"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}