{"id":3820,"date":"2019-11-01T00:01:36","date_gmt":"2019-11-01T07:01:36","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3820"},"modified":"2019-11-08T08:47:39","modified_gmt":"2019-11-08T16:47:39","slug":"the-yorn-function","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3820","title":{"rendered":"The Yorn Function"},"content":{"rendered":"<p>When a computer program wants to know a Yes or No answer, the function I write is called <em>yorn()<\/em>.<br \/>\n<!--more--><br \/>\nA <em>yorn()<\/em> function can be simple or complex. Simple functions limit the answer to one option, such as the (Y\/n) prompts in Unix\/Linux. This type of <em>yorn()<\/em> function interprets any key other than Shift+Y as &#8220;no&#8221; and the operation is canceled.<\/p>\n<p>A complex <em>yorn()<\/em> function would accept as a positive response single keys Y or y and the full word Yes, YES, and all uppercase\/lowercase combinations therein. It would similarly interpret a negative response and reject anything else as invalid, prompting for input again.<\/p>\n<p>The difficulty with writing a complex yes-or-no function is compounded by stream input: The code doesn&#8217;t just read the Y or N, it must also contend with ES and O plus the Enter key press, all of which come squirting in through standard input. Upper and lower case letters must be considered. Overflow is an issue.<\/p>\n<p>For this month&#8217;s Exercise, your task is to write a <em>yorn()<\/em> function. Here&#8217;s the skeleton you must work with:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint yorn(void)\r\n{\r\n}\r\n\r\nint main()\r\n{\r\n    int r;\r\n\r\n    while(1)\r\n    {\r\n        printf(\"Would you like to destroy the Earth?\");\r\n        r = yorn();\r\n        if( r==1 )\r\n        {\r\n            puts(\"The Earth will be destroyed.\");\r\n            break;\r\n        }\r\n        if( r==2 )\r\n        {\r\n            puts(\"The Earth will be saved.\");\r\n            break;\r\n        }\r\n        puts(\"Invalid response\");\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>yorn() <\/em>function returns two valid integers: 1 for &#8220;Yes&#8221; and 2 for &#8220;No.&#8221; In my solution, zero references invalid input, but you can return any value other than 1 or 2 for invalid input.<\/p>\n<p>Be aware that the question\/prompt lies in an endless <em>while<\/em> loop. The code&#8217;s purpose is to extract a Yes or No response. The user need type only <strong>Y<\/strong> or <strong>N<\/strong> (or <strong>y<\/strong> or <strong>n<\/strong>), but they may also type <strong>Yes<\/strong> or <strong>No<\/strong> as valid responses. Your solution must deal with these situations and repeat the prompt when input is invalid. Anticipate the worst user to ensure that the code can handle horrible and improper input.<\/p>\n<p><a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3829\">Click here<\/a> to view my solution, but please try coding a bulletproof <em>yorn()<\/em> function on your own before you see what I&#8217;ve done.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my personal programming lingo, <em>yorn<\/em> is slang for &#8220;Yes or No.&#8221; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3820\">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-3820","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\/3820","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=3820"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3820\/revisions"}],"predecessor-version":[{"id":3855,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3820\/revisions\/3855"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3820"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3820"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3820"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}