{"id":15968,"date":"2022-10-13T18:11:52","date_gmt":"2022-10-13T12:41:52","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-program-to-track-the-passing-of-a-ball-in-a-circle-of-10-people-using-boolean\/"},"modified":"2022-10-13T18:11:52","modified_gmt":"2022-10-13T12:41:52","slug":"solved-program-to-track-the-passing-of-a-ball-in-a-circle-of-10-people-using-boolean","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-program-to-track-the-passing-of-a-ball-in-a-circle-of-10-people-using-boolean\/","title":{"rendered":"[Solved] Program to track the passing of a ball in a circle of 10 people using boolean"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-64519040\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"64519040\" data-parentid=\"64518607\" data-score=\"0\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>I wrote this before the edit, so refer to your original code to see the differences. I added comments to explain what I did.<\/p>\n<p>One of the problems is the lack of <code>{}<\/code> around the code after the <code>if<\/code> statements. Without them only the first statement after the <code>if<\/code> is conditional.<\/p>\n<pre><code>if(newLocation == 0)\n      cout &lt;&lt; \"\\nPerson zero has the ball\\n\"; \/\/ Executes based on the condition\n      t0 = true; \/\/ executes always\n<\/code><\/pre>\n<p>Some languages, like Python, use indenting to match things up but C++ does not, so you need braces. I recommend getting into the habit of always using them even when there is only a single line after the conditional. IMO it makes the code easier to read, shows explicit intent, and will never turn into a bug when you add another line that should be conditional but isn&#8217;t because you didn&#8217;t have the braces.<\/p>\n<p>The other changes are pretty simple. I cleared all of the boolean variables to false before assigning the correct one to true and I used <code>%<\/code> (mod) to wrap the value around when it is increased past the end.<\/p>\n<p>I don&#8217;t use <code>using namespace std;<\/code> so I added the explicit <code>std::<\/code> prefix where needed. I recommend you get in that habit too. It may seem harmless and save you some typing, but someday you&#8217;ll regret it, especially if you ever put it in a header.<\/p>\n<p>I&#8217;m hoping that your next assignment teaches you how to do this using an array for the variables since it would be so much cleaner and simpler.<\/p>\n<pre><code>#include &lt;iostream&gt;\n\nint main()\n{\n    bool t0, t1, t2, t3, t4, t5, t6, t7, t8, t9;\n    int num1 = 0, num2 = 0;\n\n    \/\/ loop forever. The loop will exit when a negative number is entered.\n    for(;;)\n    {\n        \/\/ Set all of the variables to false\n        t0 = t1 = t2 = t3 = t4 = t5 = t6 = t7 = t8 = t9 = false;\n\n        std::cout &lt;&lt; \"Enter a number:\\n\";\n        std::cin &gt;&gt; num1;\n\n        \/\/ negative number ends the game\n        if (num1 &lt; 0)\n        {\n            std::cout &lt;&lt; \"Mom says dinner is ready!\\n\";\n            break; \/\/ exit loop\n        }\n\n        \/\/ advance the ball\n        \/\/ add the new value and use % to wrap it around\n        num2 += num1;\n        num2 %= 10;\n\n        if (num2 == 0)\n        {\n            std::cout &lt;&lt; \"\\nPerson zero has the ball\\n\";\n            t0 = true;\n        }\n        if (num2 == 1)\n        {\n            std::cout &lt;&lt; \"\\nPerson one has the ball\\n\";\n            t1 = true;\n        }\n        if (num2 == 2)\n        {\n            std::cout &lt;&lt; \"\\nPerson two has the ball\\n\";\n            t2 = true;\n        }\n        if (num2 == 3)\n        {\n            std::cout &lt;&lt; \"\\nPerson three has the ball\\n\";\n            t3 = true;\n        }\n        if (num2 == 4)\n        {\n            std::cout &lt;&lt; \"\\nPerson four has the ball\\n\";\n            t4 = true;\n        }\n        if (num2 == 5)\n        {\n            std::cout &lt;&lt; \"\\nPerson five has the ball\\n\";\n            t5 = true;\n        }\n        if (num2 == 6)\n        {\n            std::cout &lt;&lt; \"\\nPerson six has the ball\\n\";\n            t6 = true;\n        }\n        if (num2 == 7)\n        {\n            std::cout &lt;&lt; \"\\nPerson seven has the ball\\n\";\n            t7 = true;\n        }\n        if (num2 == 8)\n        {\n            std::cout &lt;&lt; \"\\nPerson eight has the ball\\n\";\n            t8 = true;\n        }\n        if (num2 == 9)\n        {\n            std::cout &lt;&lt; \"\\nPerson nine has the ball\\n\";\n            t9 = true;\n        }\n    }\n\n    return 0;\n}\n<\/code><\/pre>\n<p>It&#8217;s also possible to replace the <code>if<\/code> statements with a <code>switch<\/code> if you wanted to. For something like this it doesn&#8217;t really matter, but there are cases where it might be preferred. That would look like this:<\/p>\n<pre><code>switch (num2)\n{\ncase 0:\n    std::cout &lt;&lt; \"\\nPerson zero has the ball\\n\";\n    t0 = true;\n    break;\ncase 1:\n    std::cout &lt;&lt; \"\\nPerson zero has the ball\\n\";\n    t0 = true;\n    break;\n}\n...\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">1<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Program to track the passing of a ball in a circle of 10 people using boolean <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] I wrote this before the edit, so refer to your original code to see the differences. I added comments to explain what I did. One of the problems is the lack of {} around the code after the if statements. Without them only the first statement after the if is conditional. if(newLocation == 0) &#8230; <a title=\"[Solved] Program to track the passing of a ball in a circle of 10 people using boolean\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-program-to-track-the-passing-of-a-ball-in-a-circle-of-10-people-using-boolean\/\" aria-label=\"More on [Solved] Program to track the passing of a ball in a circle of 10 people using boolean\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[324],"class_list":["post-15968","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>[Solved] Program to track the passing of a ball in a circle of 10 people using boolean - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-program-to-track-the-passing-of-a-ball-in-a-circle-of-10-people-using-boolean\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Program to track the passing of a ball in a circle of 10 people using boolean - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] I wrote this before the edit, so refer to your original code to see the differences. I added comments to explain what I did. One of the problems is the lack of {} around the code after the if statements. Without them only the first statement after the if is conditional. if(newLocation == 0) ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-program-to-track-the-passing-of-a-ball-in-a-circle-of-10-people-using-boolean\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-13T12:41:52+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-program-to-track-the-passing-of-a-ball-in-a-circle-of-10-people-using-boolean\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-program-to-track-the-passing-of-a-ball-in-a-circle-of-10-people-using-boolean\\\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Program to track the passing of a ball in a circle of 10 people using boolean\",\"datePublished\":\"2022-10-13T12:41:52+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-program-to-track-the-passing-of-a-ball-in-a-circle-of-10-people-using-boolean\\\/\"},\"wordCount\":305,\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"keywords\":[\"c++\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-program-to-track-the-passing-of-a-ball-in-a-circle-of-10-people-using-boolean\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-program-to-track-the-passing-of-a-ball-in-a-circle-of-10-people-using-boolean\\\/\",\"name\":\"[Solved] Program to track the passing of a ball in a circle of 10 people using boolean - JassWeb\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\"},\"datePublished\":\"2022-10-13T12:41:52+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-program-to-track-the-passing-of-a-ball-in-a-circle-of-10-people-using-boolean\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-program-to-track-the-passing-of-a-ball-in-a-circle-of-10-people-using-boolean\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-program-to-track-the-passing-of-a-ball-in-a-circle-of-10-people-using-boolean\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Program to track the passing of a ball in a circle of 10 people using boolean\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\\\/\\\/jassweb.com\"],\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/author\\\/jaspritsinghghumangmail-com\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Program to track the passing of a ball in a circle of 10 people using boolean - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-program-to-track-the-passing-of-a-ball-in-a-circle-of-10-people-using-boolean\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Program to track the passing of a ball in a circle of 10 people using boolean - JassWeb","og_description":"[ad_1] I wrote this before the edit, so refer to your original code to see the differences. I added comments to explain what I did. One of the problems is the lack of {} around the code after the if statements. Without them only the first statement after the if is conditional. if(newLocation == 0) ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-program-to-track-the-passing-of-a-ball-in-a-circle-of-10-people-using-boolean\/","og_site_name":"JassWeb","article_published_time":"2022-10-13T12:41:52+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-program-to-track-the-passing-of-a-ball-in-a-circle-of-10-people-using-boolean\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-program-to-track-the-passing-of-a-ball-in-a-circle-of-10-people-using-boolean\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Program to track the passing of a ball in a circle of 10 people using boolean","datePublished":"2022-10-13T12:41:52+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-program-to-track-the-passing-of-a-ball-in-a-circle-of-10-people-using-boolean\/"},"wordCount":305,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-program-to-track-the-passing-of-a-ball-in-a-circle-of-10-people-using-boolean\/","url":"https:\/\/jassweb.com\/solved\/solved-program-to-track-the-passing-of-a-ball-in-a-circle-of-10-people-using-boolean\/","name":"[Solved] Program to track the passing of a ball in a circle of 10 people using boolean - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-10-13T12:41:52+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-program-to-track-the-passing-of-a-ball-in-a-circle-of-10-people-using-boolean\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-program-to-track-the-passing-of-a-ball-in-a-circle-of-10-people-using-boolean\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-program-to-track-the-passing-of-a-ball-in-a-circle-of-10-people-using-boolean\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Program to track the passing of a ball in a circle of 10 people using boolean"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/15968","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=15968"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/15968\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=15968"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=15968"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=15968"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}