{"id":342,"date":"2018-04-23T23:59:26","date_gmt":"2018-04-23T13:59:26","guid":{"rendered":"https:\/\/ghiassy.net\/?p=342"},"modified":"2024-03-17T19:15:51","modified_gmt":"2024-03-17T09:15:51","slug":"c-progress-bar","status":"publish","type":"post","link":"https:\/\/ghiassy.net\/?p=342","title":{"rendered":"C++ Progress Bar"},"content":{"rendered":"<p><span style=\"color: #00ff00;\">Hello,&#8230;&#8230;<\/span><\/p>\n<p><span style=\"color: #00ff00;\">One of the things that was always fascinated me was the implementation of Progress Bar with console applications.<\/span><\/p>\n<p><span style=\"color: #00ff00;\">If you ever used applications such as &#8220;wget&#8221; you know the progress bar is used to notify the user about the progress of the download or the process.<\/span><\/p>\n<p><span style=\"color: #00ff00;\">Here is the C++ source code of the Progress bar, it can also be used with C++14 threads if you application would have more than one thread.<\/span><br \/>\n<!--more--><\/p>\n<p><span style=\"color: #00ff00;\">\u00a0<\/span><\/p>\n<pre class=\"theme:mm-dark-blue lang:c++ mark:3,24 decode:true \" title=\"ProgressBar.cpp\">#include &lt;iostream&gt;\r\n#include &lt;thread&gt;\r\n#include &lt;chrono&gt;\r\n\r\nint main() {\r\n\tfloat progress = 0.0;\r\n\r\n\tstd::cout &lt;&lt; \"This is checking progress bar.\" &lt;&lt; std::endl;\r\n\r\n\twhile (progress &lt;= 1.0) {\r\n\t\tint barWidth = 50;\r\n\r\n\t\tstd::cout &lt;&lt; \"[\";\r\n\t\tint pos = barWidth * progress;\r\n\t\tfor (int i = 0; i &lt; barWidth; ++i) {\r\n\t\t\tif (i &lt; pos) std::cout &lt;&lt; \"=\";\r\n\t\t\telse if (i == pos) std::cout &lt;&lt; \"&gt;\";\r\n\t\t\telse std::cout &lt;&lt; \" \";\r\n\t\t}\r\n\t\tstd::cout &lt;&lt; \"] \" &lt;&lt; int(progress * 100.0) &lt;&lt; \" %\\r\";\r\n\t\tstd::cout.flush();\r\n\r\n\t\tprogress += 0.2; \/\/ for demonstration only\r\n\t\tstd::this_thread::sleep_for(std::chrono::milliseconds(1000));\r\n\t}\r\n\tstd::cout &lt;&lt; std::endl;\r\n\r\n\tstd::cout &lt;&lt; \"Task Completed :)\" &lt;&lt; std::endl;\r\n\r\n\treturn 0;\r\n}\r\n<\/pre>\n<p><span style=\"color: #00ff00;\">And here is the output of the program.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-343\" src=\"https:\/\/ghiassy.net\/wp-content\/uploads\/2018\/04\/ProgressBar.gif\" alt=\"\" width=\"600\" height=\"293\" \/><\/p>\n<p><span style=\"color: #00ff00;\">Hope you find this useful.<\/span><\/p>\n<p><span style=\"color: #00ff00;\">Thanks.<\/span><\/p>\n<p><span style=\"color: #00ff00;\">&#8212; Saeed \ud83d\ude42<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello,&#8230;&#8230; One of the things that was always fascinated me was the implementation of Progress Bar with console applications. If you ever used applications such as &#8220;wget&#8221; you know the progress bar is used to notify the user about the progress of the download or the process. Here is the C++ source code of the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-342","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"https:\/\/ghiassy.net\/index.php?rest_route=\/wp\/v2\/posts\/342","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ghiassy.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ghiassy.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ghiassy.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ghiassy.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=342"}],"version-history":[{"count":16,"href":"https:\/\/ghiassy.net\/index.php?rest_route=\/wp\/v2\/posts\/342\/revisions"}],"predecessor-version":[{"id":554,"href":"https:\/\/ghiassy.net\/index.php?rest_route=\/wp\/v2\/posts\/342\/revisions\/554"}],"wp:attachment":[{"href":"https:\/\/ghiassy.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=342"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ghiassy.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=342"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ghiassy.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=342"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}