{"id":1696,"date":"2016-01-01T00:01:06","date_gmt":"2016-01-01T08:01:06","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1696"},"modified":"2016-01-08T21:10:42","modified_gmt":"2016-01-09T05:10:42","slug":"defensive-input","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1696","title":{"rendered":"Defensive Input"},"content":{"rendered":"<p>The task is rather simple: Prompt the user to input a value between 1 and 9. If you&#8217;ve read the first few chapters of any of my <em>For Dummies<\/em> C programming books, you could do that one easily. But what happens when the user doesn&#8217;t type a value in that range or &mdash; worse &mdash; they type text instead?<br \/>\n<!--more--><br \/>\nBack in the day, programmers would call the technique &#8220;bullet-proofing.&#8221; It&#8217;s the process of preventing code from running amok by planning for all possibilities.<\/p>\n<p>I remember beta testers &mdash; and these were <em>real<\/em> beta testers and not merely early adopters &mdash; who would go to every prompt in a program and type any possible input to test the code.<\/p>\n<p>For example, a configuration option prompted the user had to enter a value in the range of 1 to 9. One beta tester discovered that if you typed two dozen 9s that the code would crash. <em>That<\/em> is good beta testing. While it might seem ridiculous for anyone familiar with the program to do something other than what the prompt says, users aren&#8217;t obedient.<\/p>\n<p>For this month&#8217;s Exercise, you need to write a simple prompt that asks for a value in the range of 1 to 9. The assumption is that the user will type in anything from a super huge value to text to nothing at all. The code must evaluate the input, confirm whether or not it&#8217;s proper, and ask for input again if the current input is invalid.<\/p>\n<p>Here is a sample run of my solution, which will help you get an idea of what I&#8217;m after:<\/p>\n<pre><code>Enter a number 1 to 9: <span style=\"color:black\">This is a test<\/span>\r\nIncorrect input. Try again:\r\nEnter a number 1 to 9: <span style=\"color:black\">888888888888<\/span>\r\nIncorrect input. Try again:\r\nEnter a number 1 to 9: <span style=\"color:black\">5<\/span>\r\nYou input 5\r\nThank you!<\/code><\/pre>\n<p>Several methods exist for solving this puzzle. Be sure to test all kinds of whacky input to bullet-proof your code. <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1701\">Click here<\/a> to read my solution, but I encourage you to try this Exercise on your own before seeing what I did.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Coding becomes more of a challenge when you have unruly users. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1696\">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-1696","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\/1696","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=1696"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1696\/revisions"}],"predecessor-version":[{"id":1724,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1696\/revisions\/1724"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1696"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1696"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1696"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}