{"id":5459,"date":"2022-07-30T00:01:30","date_gmt":"2022-07-30T07:01:30","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5459"},"modified":"2022-07-23T11:09:53","modified_gmt":"2022-07-23T18:09:53","slug":"forcing-a-decision","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5459","title":{"rendered":"Forcing a Decision"},"content":{"rendered":"<p>Programs often ask questions: yes-or-no and multiple choice. The user <em>must<\/em> select one of the given options, which I refer to as forcing a decision. Your goal as programmer is to pester the user to input a valid choice.<br \/>\n<!--more--><br \/>\nThe easy and common way to resolve this pickle is to select a default option and, when the user is goofy, the default is selected.<\/p>\n<p>For example, Linux installation scripts offer the user a Yes or No (what I call a &#8220;yorn&#8221;) option. If the user types anything other than Y or sometimes Y-E-S, computer says, &#8220;Okay, you mean &#8216;no'&#8221; and proceeds. This approach doesn&#8217;t really force a decision; it presents a default.<\/p>\n<p>My approach for forcing a decision is to bury a <em>switch-case<\/em> structure within an endless <em>while<\/em> loop. A specific variable is required to ensure that the loop repeats until a correct choice is made. I consistently name this variable <code>done<\/code> because it makes the code read well:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_07_30-Lesson.c\" rel=\"noopener\" target=\"_blank\">2022_07_30-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    const int true=1,false=0;\r\n    int ch,done;\r\n\r\n    <span class=\"comments\">\/* prepare for the loop *\/<\/span>\r\n    done = false;    <span class=\"comments\">\/* the loop is not done at this point *\/<\/span>\r\n\r\n    while(!done)    <span class=\"comments\">\/* \"while not done\" because\r\n                       !done (not done) == TRUE *\/<\/span>\r\n    {\r\n        printf(\"You must choose A, B, or C: \");\r\n        ch = getchar();\r\n\r\n        <span class=\"comments\">\/* check their results *\/<\/span>\r\n        switch(ch)\r\n        {\r\n            <span class=\"comments\">\/* check for both upper- and lowercase *\/<\/span>\r\n            case 'A':\r\n            case 'a':\r\n                <span class=\"comments\">\/* do whatever *\/<\/span>\r\n                puts(\"Carrying out option A\");\r\n                <span class=\"comments\">\/* end the loop condition *\/<\/span>\r\n                done = true;\r\n                break;\r\n            case 'B':\r\n            case 'b':\r\n                puts(\"Carrying out option B\");\r\n                done = true;\r\n                break;\r\n            case 'C':\r\n            case 'c':\r\n                puts(\"Carrying out option C\");\r\n                done = true;\r\n                break;\r\n            default:\r\n                puts(\"Invalid choice\");\r\n                <span class=\"comments\">\/* empty the stdin buffer *\/<\/span>\r\n                while( getchar()!='\\n' )\r\n                    ;\r\n        }\r\n    }\r\n    puts(\"Thank you for behaving\");\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Variable <code>done<\/code> is initially set to FALSE (zero). As the <em>while<\/em> loop&#8217;s condition is <code>!done<\/code> (&#8220;not done&#8221;), it translates to TRUE, so the loop repeats until the value of <code>done<\/code> becomes FALSE.<\/p>\n<p>Within the <em>while<\/em> loop, <em>switch-case<\/em> statements filter possible correct responses. If any of the six valid choices are input, the code outputs a confirmation text message (or does something more interesting in real life) and the value of <code>done<\/code> becomes TRUE. The loop is done.<\/p>\n<p>The <em>default<\/em> condition handles any invalid input. An error message is output, then an inner <em>while<\/em> loop churns through whatever text lingers in the <em>stdin<\/em> input buffer until a newline is encountered:<\/p>\n<p><code>while( getchar()!='\\n' )<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;;<\/code><\/p>\n<p>This statement gobbles the newline as well, which makes stream input behave in a familiar manner for the user.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<p><code>You must choose A, B, or C: hello<br \/>\nInvalid choice<br \/>\nYou must choose A, B, or C: d<br \/>\nInvalid choice<br \/>\nYou must choose A, B, or C: a<br \/>\nCarrying out option A<br \/>\nThank you for behaving<\/code><\/p>\n<p>I wrote <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3820\">an Exercise<\/a> years back that dealt with a similar situation, though only for &#8220;yorn&#8221; responses. It seems a bit clunky now that I look in the rearview mirror. The method presented in this post is far more elegant and can easily be adapted to any number of options for forcing a decision.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>My solution to a common programming query. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5459\">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":[2],"tags":[],"class_list":["post-5459","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5459","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=5459"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5459\/revisions"}],"predecessor-version":[{"id":5471,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5459\/revisions\/5471"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5459"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5459"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5459"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}