<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-1543848439127765809</id><updated>2024-09-14T03:17:22.489-04:00</updated><category term="C++"/><category term="Beginner"/><category term="Console Window"/><category term="Intermediate"/><title type='text'>C++ Monastery</title><subtitle type='html'>The most interesting and useful programming tasks, in C++.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://cppmonastery.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1543848439127765809/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://cppmonastery.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>TickNSwisted</name><uri>http://www.blogger.com/profile/10882833507254467617</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>7</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-1543848439127765809.post-7472813239866743733</id><published>2019-05-01T11:26:00.001-04:00</published><updated>2019-05-01T11:26:03.712-04:00</updated><title type='text'>EvenOdd PrimeComposite PositiveNegative Factors</title><content type='html'>&lt;style&gt;

#codeTable
{
    font-family: Consolas;
    font-style: normal;
    font-weight: normal;
    border:2px solid black;
    border-style: solid;
    border-color: #000000;
    background-color: white;
    text-align: left;
    color: black;

}

#codeOutput
{   
    padding-top:30px;
    padding-bottom:30px;
    padding-right:30px;
    padding-left:30px;
    font-family: &quot;Lucida Console&quot;, Courier New, Serif
    font-style: normal;
    font-weight: normal;
    font-size: 12 px;
    background-color: black;
    color: #cdcdcd;
    border:2px solid #cdcdcd;
    border-style: solid;
    border-color: #cdcdcd;
    background-color: #000000;
    text-align: left;
    color: #cdcdcd;
}

#codeBRef
{
    font-family: Consolas;
    font-style: normal;
    font-weight: normal;
    text-align: left;
    color: #3399FF;
}

#codeNRef
{
    font-family: Consolas;
    font-style: normal;
    font-weight: normal;
    text-align: left;
    color: white;
}

#codeTitle
{
    background-color: dark gray;
    font-family: Arial;
    font-style: italic;
    font-weight: bold;
    font-size: 12px;
    text-align: center;
    color: black;
}

#codeFile
{
    font-family: &quot;Lucida Console&quot;, Courier New, Serif
    font-style: normal;
    font-weight: normal;
    font-size: 12 px;
    background-color: black;
    text-align:center;
    color: yellow;
}


#codeN
{
    font-family: Consolas;
    font-style: normal;
    font-weight: normal;
    text-align: left;
    color: black;
}

#codeG
{
    font-family: Consolas;
    font-style: normal;
    font-weight: normal;
    text-align: left;
    color: green;
}

#codeR
{
    font-family: Consolas;
    font-style: normal;
    font-weight: normal;
    text-align: left;
    color: #a31515;
}

#codeB
{
    font-family: Consolas;
    font-style: normal;
    font-weight: normal;
    text-align: left;
    color: blue;
}

#codeT
{
    font-family: Consolas;
    font-style: normal;
    font-weight: normal;
    text-align: left;
    color: teal;
}

&lt;/style&gt;
It can be useful to know all sorts of details about a number in a program. This C++ code determines if a number is Positive or Negative, Even or Odd, Prime or Composite, and finally, if a positive number, the program will output the factors of the number.&lt;br /&gt;
A summary of the operations:
Positive or Negative:
number &amp;gt; 0
Even or Odd:
number % 2 == 0
Prime or Composite:


&lt;br /&gt;
&lt;table id=&quot;codeTable&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;
&lt;td id=&quot;codeTitle&quot;&gt;Even Odd Prime Composite Positive Negative Factors&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td id=&quot;codeFile&quot;&gt;main.cpp&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td id=&quot;CodeTable&quot;&gt;&lt;span id=&quot;codeB&quot;&gt;#include&lt;/span&gt;&amp;nbsp;&lt;span id=&quot;codeR&quot;&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;#include&lt;/span&gt;&amp;nbsp;&lt;span id=&quot;codeR&quot;&gt;&amp;lt;vector&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;#include&lt;/span&gt;&amp;nbsp;&lt;span id=&quot;codeR&quot;&gt;&amp;lt;string&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;using namespace&lt;/span&gt; std;&lt;br /&gt;
&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; main(&lt;span id=&quot;codeB&quot;&gt;void&lt;/span&gt;)&lt;br /&gt;
{&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;&amp;nbsp; &amp;nbsp; long long int&lt;/span&gt; number = 0;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//number input by user&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeB&quot;&gt;unsigned long long int&lt;/span&gt; count = 0;&amp;nbsp; &amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//count of factors&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span id=&quot;codeT&quot;&gt;vector&lt;/span&gt;&amp;lt;&lt;span id=&quot;codeB&quot;&gt;unsigned int long&lt;/span&gt;&amp;gt;factor;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;bool&lt;/span&gt; badInput = &lt;span id=&quot;codeB&quot;&gt;false&lt;/span&gt;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;bool&lt;/span&gt; isNumberNegative = &lt;span id=&quot;codeB&quot;&gt;false&lt;/span&gt;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span id=&quot;codeT&quot;&gt;string&lt;/span&gt; tempInput;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;do&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//HANDLE IF USER INPUT A NON-NUMBER&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;if&lt;/span&gt; (badInput == &lt;span id=&quot;codeB&quot;&gt;true&lt;/span&gt;)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cout &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&quot;Bad user input.\n&quot;&lt;/span&gt;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; tempInput.clear();&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cin.clear(); &amp;nbsp; &amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//clear error flag&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//ignore entire input&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cin.ignore(&lt;span id=&quot;codeT&quot;&gt;numeric_limits&lt;/span&gt;&amp;lt;&lt;span id=&quot;codeT&quot;&gt;streamsize&lt;/span&gt;&amp;gt;::max(), &lt;span id=&quot;codeR&quot;&gt;&#39;\n&#39;&lt;/span&gt;);&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//PROMPT USER FOR NUMBER&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cout &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&quot;Enter a whole number to examine: &quot;&lt;/span&gt;;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//get user input to tempInput&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cin &amp;gt;&amp;gt; tempInput;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; badInput = &lt;span id=&quot;codeB&quot;&gt;false&lt;/span&gt;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//loop through each character of tempInput&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;for&lt;/span&gt; (unsigned int i = 0; i &amp;lt; tempInput.length(); i++)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//test if character is digit&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;if&lt;/span&gt; (!isdigit(tempInput.at(i)))&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//if fails digit check set bad input flag and escape&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; badInput = &lt;span id=&quot;codeB&quot;&gt;true&lt;/span&gt;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//breaks us out of for loop, we could also set i to length + 1&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;break&lt;/span&gt;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;else&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; badInput = &lt;span id=&quot;codeB&quot;&gt;false&lt;/span&gt;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//having parsed the user input, store it as a number&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;if&lt;/span&gt; (badInput == &lt;span id=&quot;codeB&quot;&gt;false&lt;/span&gt;)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; number = stoll(tempInput);&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; } &lt;span id=&quot;codeB&quot;&gt;while&lt;/span&gt; (badInput == &lt;span id=&quot;codeB&quot;&gt;true&lt;/span&gt;);&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; cout &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&#39;\n&#39;&lt;/span&gt; &amp;lt;&amp;lt; number &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&quot; is &quot;&lt;/span&gt;;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//TEST IF NEGATIVE OR POSITIVE&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;if&lt;/span&gt; (number &amp;lt; 0)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cout &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&quot;negative&quot;&lt;/span&gt;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; isNumberNegative = &lt;span id=&quot;codeB&quot;&gt;true&lt;/span&gt;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;else if&lt;/span&gt; ( number &amp;gt; 0)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cout &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&quot;positive&quot;&lt;/span&gt;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;else&lt;/span&gt; &lt;span id=&quot;codeG&quot;&gt;// must be zero&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cout &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&quot;neither negative or positive&quot;&lt;/span&gt;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//TEST IF EVEN OR ODD&lt;br /&gt;
&amp;nbsp; &amp;nbsp; //since even numbers are divisible by two with no remainder,&lt;br /&gt;
&amp;nbsp; &amp;nbsp; //we use modulus to check if number divided by 2 has remainder.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;if&lt;/span&gt; (number % 2 == 0)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cout &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&quot;, is even, &quot;&lt;/span&gt;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;else&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cout &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&quot;, is odd, &quot;&lt;/span&gt;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//TEST IF PRIME OR COMPOSITE&lt;br /&gt;
&amp;nbsp; &amp;nbsp; // prime is a positive integer that has exactly two positive divisors&lt;br /&gt;
&amp;nbsp; &amp;nbsp; // 0 and 1 are neither prime nor composite&lt;br /&gt;
&amp;nbsp; &amp;nbsp; //Negative numbers are not prime or composite, so we don&#39;t test.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;if&lt;/span&gt; (isNumberNegative || number == 0 || number == 1)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cout &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&quot;is not prime or composite&quot;&lt;/span&gt;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;else&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//go through each integer, from one to the user&#39;s number.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;for&lt;/span&gt; (&lt;span id=&quot;codeB&quot;&gt;long long int&lt;/span&gt; i = 1; i &amp;lt; number; i++)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//test if user&#39;s number is divisible by current iteration (i).&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;if&lt;/span&gt; (number % i == 0)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//increment the count of factors.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; count++;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//the current number is a factor, store it in the stack&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; factor.push_back(i);&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//TEST IF COMPOSITE OR PRIME&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; //if the count of factors is more than one, it must be composite.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;if&lt;/span&gt; (count &amp;gt; 1)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cout &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&quot;composite&quot;&lt;/span&gt;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;else&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cout &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&quot;prime&quot;&lt;/span&gt;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//OUTPUT FACTORS&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; cout &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&quot; and has &quot;&lt;/span&gt; &amp;lt;&amp;lt; factor.size() &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&quot; factors:\n&quot;&lt;/span&gt;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;for&lt;/span&gt; (&lt;span id=&quot;codeB&quot;&gt;unsigned int&lt;/span&gt; i = 0; i &amp;lt; factor.size(); i++)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cout &amp;lt;&amp;lt; factor.at(i) &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&#39;\n&#39;&lt;/span&gt;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//delete factors, if any, - tidy up&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; factor.clear();&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//holds screen so we can view results&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; cout &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&quot;\nEnter character and press enter to exit.&quot;&lt;/span&gt;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; cin &amp;gt;&amp;gt; number;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;return&lt;/span&gt; 0;&lt;br /&gt;
}&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://cppmonastery.blogspot.com/feeds/7472813239866743733/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cppmonastery.blogspot.com/2019/05/evenodd-primecomposite-positivenegative.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1543848439127765809/posts/default/7472813239866743733'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1543848439127765809/posts/default/7472813239866743733'/><link rel='alternate' type='text/html' href='http://cppmonastery.blogspot.com/2019/05/evenodd-primecomposite-positivenegative.html' title='EvenOdd PrimeComposite PositiveNegative Factors'/><author><name>TickNSwisted</name><uri>http://www.blogger.com/profile/10882833507254467617</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1543848439127765809.post-7745916084954120429</id><published>2019-05-01T09:10:00.001-04:00</published><updated>2019-05-01T09:10:04.990-04:00</updated><title type='text'>Command Line Arguments Revamped</title><content type='html'>&lt;style&gt;
#codeTable
{
    font-family: Consolas;
    font-style: normal;
    font-weight: normal;
    border:2px solid black;
    border-style: solid;
    border-color: #000000;
    background-color: white;
    text-align: left;
    color: black;

}

#codeOutput
{   
    padding-top:30px;
    padding-bottom:30px;
    padding-right:30px;
    padding-left:30px;
    font-family: &quot;Lucida Console&quot;, Courier New, Serif
    font-style: normal;
    font-weight: normal;
    font-size: 12 px;
    background-color: black;
    color: #cdcdcd;
    border:2px solid #cdcdcd;
    border-style: solid;
    border-color: #cdcdcd;
    background-color: #000000;
    text-align: left;
    color: #cdcdcd;
}

#codeBRef
{
    font-family: Consolas;
    font-style: normal;
    font-weight: normal;
    text-align: left;
    color: #3399FF;
}

#codeNRef
{
    font-family: Consolas;
    font-style: normal;
    font-weight: normal;
    text-align: left;
    color: white;
}

#codeTitle
{
    background-color: dark gray;
    font-family: Arial;
    font-style: italic;
    font-weight: bold;
    font-size: 12px;
    text-align: center;
    color: black;
}

#codeFile
{
    font-family: &quot;Lucida Console&quot;, Courier New, Serif
    font-style: normal;
    font-weight: normal;
    font-size: 12 px;
    background-color: black;
    text-align:center;
    color: yellow;
}


#codeN
{
    font-family: Consolas;
    font-style: normal;
    font-weight: normal;
    text-align: left;
    color: black;
}

#codeG
{
    font-family: Consolas;
    font-style: normal;
    font-weight: normal;
    text-align: left;
    color: green;
}

#codeR
{
    font-family: Consolas;
    font-style: normal;
    font-weight: normal;
    text-align: left;
    color: #a31515;
}

#codeB
{
    font-family: Consolas;
    font-style: normal;
    font-weight: normal;
    text-align: left;
    color: blue;
}
&lt;/style&gt;
Revisiting the Command Line Argument article, there are some more enhancements can be made to
the longevity of the code. For example, we can make a typedef for a return code so that if we
want to change the type, we can do it in one location and have all uses of it change as well.&lt;br /&gt;&lt;br /&gt;
We can also offload the parsing of the command line into its own function, away from cluttering up the main. The trade off is that adding a new recognized argument must take into account the struct CommandLineArgs_t, having an associated Boolean flag.&lt;br /&gt;

&lt;br /&gt;
&lt;table id=&quot;codeTable&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;
&lt;td id=&quot;codeTitle&quot;&gt;Reading Command Line Arguments&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td id=&quot;codeFile&quot;&gt;main.cpp&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td id=&quot;CodeTable&quot;&gt;&lt;span id=&quot;codeG&quot;&gt;//----------------------------------INCLUDES-----------------------------------&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;#include&lt;/span&gt;&amp;nbsp;&lt;span id=&quot;codeR&quot;&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;#include&lt;/span&gt;&amp;nbsp;&lt;span id=&quot;codeR&quot;&gt;&amp;lt;string&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;using namespace&lt;/span&gt;&amp;nbsp;std;&lt;br /&gt;
&lt;span id=&quot;codeG&quot;&gt;//----------------------------------TYPEDEFS-----------------------------------&lt;br /&gt;
//return_code explicitly for program and associated return codes&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;typedef int&lt;/span&gt;&amp;nbsp;returnCode_t;&lt;br /&gt;
&lt;br /&gt;
&lt;span id=&quot;codeG&quot;&gt;//argumentsCount defined for argc&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;typedef unsigned int&lt;/span&gt;&amp;nbsp;argsCount_t;&lt;br /&gt;
&lt;br /&gt;
&lt;span id=&quot;codeG&quot;&gt;//command line argument flags&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;struct&lt;/span&gt;&amp;nbsp;CommandLineArgs_t {&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeB&quot;&gt;bool&lt;/span&gt; bDev;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeB&quot;&gt;bool&lt;/span&gt; bAdmin;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
&lt;span id=&quot;codeG&quot;&gt;//-----------------------------FUNCTION PROTOTYPES-----------------------------&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;void&lt;/span&gt; ParseCommandLineArguments(argsCount_t argc, &lt;span id=&quot;codeB&quot;&gt;char&lt;/span&gt;** argv, CommandLineArgs_t &amp;amp;cla);&lt;br /&gt;
&lt;br /&gt;
&lt;span id=&quot;codeG&quot;&gt;//----------------------------------MAIN ENTRY---------------------------------&lt;/span&gt;&lt;br /&gt;
returnCode_t main( argsCount_t argc, &lt;span id=&quot;codeB&quot;&gt;char&lt;/span&gt;** argv ) {&lt;br /&gt;
&lt;span id=&quot;codeG&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//return code set to one and explicitly changed just prior to normal exit&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;returnCode_t isError = 1;&lt;br /&gt;
&lt;span id=&quot;codeG&quot;&gt;&amp;nbsp; &amp;nbsp; //used for pausing just prior to exit&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;&amp;nbsp; &amp;nbsp; char&lt;/span&gt;&amp;nbsp;pause;&lt;br /&gt;
&lt;br /&gt;
&lt;span id=&quot;codeG&quot;&gt;&amp;nbsp; &amp;nbsp; //check command line arguments&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; CommandLineArgs_t commandLineArgs;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; ParseCommandLineArguments( argc, argv, commandLineArgs );&lt;br /&gt;&lt;br /&gt;
&lt;span id=&quot;codeG&quot;&gt;&amp;nbsp; &amp;nbsp; //report command line arguments to user&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;&amp;nbsp; &amp;nbsp; if&lt;/span&gt; ( commandLineArgs.bDev ) {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cout &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&quot;DEV_MODE is ON\n&quot;&lt;/span&gt;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;&amp;nbsp; &amp;nbsp; if&lt;/span&gt; ( commandLineArgs.bAdmin ) {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cout &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&quot;ADMIN is ON\n&quot;&lt;/span&gt;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&lt;span id=&quot;codeG&quot;&gt;&amp;nbsp; &amp;nbsp; //normal operations&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;&amp;nbsp; &amp;nbsp; if&lt;/span&gt;&amp;nbsp;(&amp;nbsp;commandLineArgs.bDev&amp;nbsp;) {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cout &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&quot;All mode output.\n&quot;&lt;/span&gt;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; }&lt;br /&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; cout &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&quot;Enter any character to exit: &quot;&lt;/span&gt;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; cin &amp;gt;&amp;gt; pause;&lt;br /&gt;&lt;br /&gt;
&lt;span id=&quot;codeG&quot;&gt;&amp;nbsp; &amp;nbsp; //set isError to zero just prior to normal exit&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; isError = 0;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;&amp;nbsp; &amp;nbsp; return&lt;/span&gt; isError;&lt;br /&gt;
}&lt;br /&gt;
&lt;span id=&quot;codeG&quot;&gt;
//=============================FUNCTION DEFINITIONS============================&lt;br /&gt;
//-----------------------------------------------------------------------------&lt;br /&gt;
//Function:  ParseCommandLineArguments&lt;br /&gt;
//Description: Parses command line arguments, argv, of count, argc. Values&lt;br /&gt;
//&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;are set in the passed CommandLineArgs_t, which can then be&lt;br /&gt;
//&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;read from outside this function to determine if options&lt;br /&gt;
//&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;such as dev or admin mode are enabled.&lt;br /&gt;
//-----------------------------------------------------------------------------&lt;br /&gt;
&lt;/span&gt;
void ParseCommandLineArguments( argsCount_t argc, &lt;span id=&quot;codeB&quot;&gt;char&lt;/span&gt;** argv, CommandLineArgs_t &amp;amp;cla ) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeG&quot;&gt;//argc is the count of arguments passed&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeG&quot;&gt;//argv is each of the arguments &lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeG&quot;&gt;&amp;nbsp; &amp;nbsp; //initialize default values to zero&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cla.bDev&amp;nbsp;=&amp;nbsp;&lt;span id=&quot;codeB&quot;&gt;false&lt;/span&gt;;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cla.bAdmin&amp;nbsp;=&amp;nbsp;&lt;span id=&quot;codeB&quot;&gt;false&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span id=&quot;codeG&quot;&gt;&amp;nbsp; &amp;nbsp; //loop through all arguments passed and apply it to cla&#39;s members, as applicable&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;&amp;nbsp; &amp;nbsp; for&lt;/span&gt;( argsCount_t i = 0 ; i &amp;lt; argc ; ++i ) {&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if&lt;/span&gt; (&amp;nbsp;argv[i] == &lt;span id=&quot;codeR&quot;&gt;&quot;-dev&quot;&lt;/span&gt; || &lt;span id=&quot;codeR&quot;&gt;&quot;dev&quot;&lt;/span&gt; ) {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cla.bDev = &lt;span id=&quot;codeB&quot;&gt;true&lt;/span&gt;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if&lt;/span&gt; (&amp;nbsp;argv[i] == &lt;span id=&quot;codeR&quot;&gt;&quot;-admin&quot;&lt;/span&gt; || &lt;span id=&quot;codeR&quot;&gt;&quot;admin&quot;&lt;/span&gt; ) {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cla.bAdmin = &lt;span id=&quot;codeB&quot;&gt;true&lt;/span&gt;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;&amp;nbsp; &amp;nbsp; if&lt;/span&gt; ( cla.bDev == &lt;span id=&quot;codeB&quot;&gt;true&lt;/span&gt; ) {&lt;br /&gt;
&lt;span id=&quot;codeG&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; //0th element of argv is program&#39;s path and name (or null if OS moody)&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if&lt;/span&gt; ( argv[0][0] != NULL ) {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cout &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&quot;Path reported: &quot;&lt;/span&gt; &amp;lt;&amp;lt; argv[0] &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&#39;\n&#39;&lt;/span&gt;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&amp;nbsp; &amp;nbsp; cout &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&quot;Total arguments passed:&quot;&lt;/span&gt; &amp;lt;&amp;lt; argc &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&#39;\n&#39;&lt;/span&gt;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; }&lt;br /&gt;
}&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://cppmonastery.blogspot.com/feeds/7745916084954120429/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cppmonastery.blogspot.com/2019/05/command-line-arguments-revamped.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1543848439127765809/posts/default/7745916084954120429'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1543848439127765809/posts/default/7745916084954120429'/><link rel='alternate' type='text/html' href='http://cppmonastery.blogspot.com/2019/05/command-line-arguments-revamped.html' title='Command Line Arguments Revamped'/><author><name>TickNSwisted</name><uri>http://www.blogger.com/profile/10882833507254467617</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1543848439127765809.post-3664999230080805704</id><published>2013-01-25T00:00:00.000-05:00</published><updated>2019-05-01T07:39:51.356-04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C++"/><category scheme="http://www.blogger.com/atom/ns#" term="Intermediate"/><title type='text'>Command Line Arguments</title><content type='html'>&lt;style&gt;

#codeTable
{
    font-family: Consolas;
    font-style: normal;
    font-weight: normal;
    border:2px solid black;
    border-style: solid;
    border-color: #000000;
    background-color: white;
    text-align: left;
    color: black;

}

#codeOutput
{   
    padding-top:30px;
    padding-bottom:30px;
    padding-right:30px;
    padding-left:30px;
    font-family: &quot;Lucida Console&quot;, Courier New, Serif
    font-style: normal;
    font-weight: normal;
    font-size: 12 px;
    background-color: black;
    color: #cdcdcd;
    border:2px solid #cdcdcd;
    border-style: solid;
    border-color: #cdcdcd;
    background-color: #000000;
    text-align: left;
    color: #cdcdcd;
}

#codeBRef
{
    font-family: Consolas;
    font-style: normal;
    font-weight: normal;
    text-align: left;
    color: #3399FF;
}

#codeNRef
{
    font-family: Consolas;
    font-style: normal;
    font-weight: normal;
    text-align: left;
    color: white;
}

#codeTitle
{
    background-color: dark gray;
    font-family: Arial;
    font-style: italic;
    font-weight: bold;
    font-size: 12px;
    text-align: center;
    color: black;
}

#codeFile
{
    font-family: &quot;Lucida Console&quot;, Courier New, Serif
    font-style: normal;
    font-weight: normal;
    font-size: 12 px;
    background-color: black;
    text-align:center;
    color: yellow;
}


#codeN
{
    font-family: Consolas;
    font-style: normal;
    font-weight: normal;
    text-align: left;
    color: black;
}

#codeG
{
    font-family: Consolas;
    font-style: normal;
    font-weight: normal;
    text-align: left;
    color: green;
}

#codeR
{
    font-family: Consolas;
    font-style: normal;
    font-weight: normal;
    text-align: left;
    color: #a31515;
}

#codeB
{
    font-family: Consolas;
    font-style: normal;
    font-weight: normal;
    text-align: left;
    color: blue;
}
&lt;/style&gt;
The ability to accept command line arguments is useful if a program is to have a developer mode or debug mode that wouldn&#39;t be suitable in a separate configuration file or in an options menu. By inserting command line arguments in a Window&#39;s shortcuts, each shortcut can specify different options, while still pointing to the same executable.&lt;br /&gt;
&lt;br /&gt;
A program ran with a &lt;span id=&quot;codeNRef&quot;&gt;-h&lt;/span&gt; or &lt;span id=&quot;codeNRef&quot;&gt;/?&lt;/span&gt; argument can provide a standard of assistance to  users.&lt;br /&gt;
&lt;br /&gt;
In order for a program to access its command line arguments, the &lt;span id=&quot;codeNRef&quot;&gt;main&lt;/span&gt; or &lt;span id=&quot;codeNRef&quot;&gt;winmain&lt;/span&gt;&amp;nbsp;function will need a parameter list other than the usual &lt;span id=&quot;codeNRef&quot;&gt;main ( &lt;/span&gt;&lt;span id=&quot;codeBRef&quot;&gt;void&lt;/span&gt;&lt;span id=&quot;codeNRef&quot;&gt; )&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;h4&gt;
Parsing Command Line Arguments&lt;/h4&gt;
&lt;br /&gt;
To gain access ot the command line arguments the main function will need to accept the paramters passed in by the operating system. The first parameter, generally &lt;span id=&quot;codeBRef&quot;&gt;int&lt;/span&gt; &lt;span id=&quot;codeNRef&quot;&gt;argc&lt;/span&gt;, is the number of arguments passed. The second parameter, &lt;span id=&quot;codeBRef&quot;&gt;char&lt;/span&gt;&lt;span id=&quot;codeNRef&quot;&gt;* argv[ ]&lt;/span&gt; or &lt;span id=&quot;codeBRef&quot;&gt;char&lt;/span&gt;&lt;span id=&quot;codeNRef&quot;&gt;** argv&lt;/span&gt;, is the argument. The &lt;span id=&quot;codeNRef&quot;&gt;argc&lt;/span&gt; and &lt;span id=&quot;codeNRef&quot;&gt;argv&lt;/span&gt; are the conventional names, though they can be any other valid variable name.&lt;br /&gt;
&lt;br /&gt;
There will always be at least one argument count. The first argument is the &lt;span id=&quot;codeNRef&quot;&gt;path\filename&lt;/span&gt;, similar to &lt;span id=&quot;codeNRef&quot;&gt;C:\MyFolder\MyProgram.exe&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;table id=&quot;codeTable&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;
&lt;td id=&quot;codeTitle&quot;&gt;Reading Command Line Arguments&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td id=&quot;codeFile&quot;&gt;main.cpp&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td id=&quot;CodeTable&quot;&gt;&lt;span id=&quot;codeG&quot;&gt;//===================================INCLUDES==================================&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;#include&lt;/span&gt; &lt;span id=&quot;codeR&quot;&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;using namespace&lt;/span&gt; std;&lt;br /&gt;
&lt;br /&gt;
&lt;span id=&quot;codeG&quot;&gt;//==================================MAIN ENTRY=================================&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; main ( &lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; argc, &lt;span id=&quot;codeB&quot;&gt;char&lt;/span&gt;* argv[] )&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeG&quot;&gt;//Output the total number of command line arguments, argc.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cout &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&quot;There are &quot;&lt;/span&gt; &amp;lt;&amp;lt; argc &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&quot; arguments.&quot;&lt;/span&gt; &amp;lt;&amp;lt; endl;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeG&quot;&gt;//For each argument, argc.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeB&quot;&gt;for&lt;/span&gt; ( int i = 0; i &amp;lt; argc; i++ )&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeG&quot;&gt;//Output the argument.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cout &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&quot;Argument &quot;&lt;/span&gt; &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&quot;: &quot;&lt;/span&gt; &amp;lt;&amp;lt; argv[i] &amp;lt;&amp;lt; endl;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeB&quot;&gt;return&lt;/span&gt; ( 0 );&lt;br /&gt;
}&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
The code above outputs the argument count and the arguments present. For information on running an executable with arguments, see &lt;em&gt;Setting Command Line Arguments&lt;/em&gt;, below. If we set the command line argument to &lt;span id=&quot;codeNRef&quot;&gt;-doSomething&lt;/span&gt; then we should get the following output from the Reading Command Line Arguments example.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgAgtxRvfUiDN0kOB9N9gmAOagKqBysZt0mMuAxVoWdTjow8ECKpqWncauHa0dc0vA8qwR-01OJv2SipvzGhr9-ERWD9KcsUhZbSRmcUY2txbGYocQ4gfX-d75hAhQX-yvjxYywtVnXv9o2/s1600/CmdLineOut1Lrg.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgAgtxRvfUiDN0kOB9N9gmAOagKqBysZt0mMuAxVoWdTjow8ECKpqWncauHa0dc0vA8qwR-01OJv2SipvzGhr9-ERWD9KcsUhZbSRmcUY2txbGYocQ4gfX-d75hAhQX-yvjxYywtVnXv9o2/s400/CmdLineOut1Lrg.png&quot; height=&quot;102&quot; width=&quot;400&quot;&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Having the argument parameters at run time, the program can react depending on the arguments passed in.

&lt;br /&gt;
&lt;table id=&quot;codeTable&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;
&lt;td id=&quot;codeTitle&quot;&gt;Parse Command Line Arguments&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td id=&quot;codeFile&quot;&gt;main.cpp&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td id=&quot;CodeTable&quot;&gt;&lt;span id=&quot;codeG&quot;&gt;//===================================INCLUDES==================================&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;#include&lt;/span&gt; &lt;span id=&quot;codeR&quot;&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;#include&lt;/span&gt; &lt;span id=&quot;codeR&quot;&gt;&amp;lt;string&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;using namespace&lt;/span&gt; std;&lt;br /&gt;
&lt;br /&gt;
&lt;span id=&quot;codeG&quot;&gt;//==================================MAIN ENTRY=================================&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; main ( &lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; argc, &lt;span id=&quot;codeB&quot;&gt;char&lt;/span&gt;* argv[] )&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeG&quot;&gt;//Test if argument count is more than the path/filename.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeB&quot;&gt;if&lt;/span&gt; ( argc &amp;gt; 1 )&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeG&quot;&gt;//For each argument entered, after the first.&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeB&quot;&gt;for&lt;/span&gt; ( &lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; i = 1; i &amp;lt; argc; i++ )&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeG&quot;&gt;//Test a certain condition.&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeB&quot;&gt;if&lt;/span&gt; ( string( argv[i] ) == &lt;span id=&quot;codeR&quot;&gt;&quot;-dev&quot;&lt;/span&gt; )&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cout &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&quot;Developer Mode Activated!&quot;&lt;/span&gt; &amp;lt;&amp;lt; endl;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeG&quot;&gt;//Test another condition.&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeB&quot;&gt;if&lt;/span&gt; ( string( argv[i] ) == &lt;span id=&quot;codeR&quot;&gt;&quot;-cheats&quot;&lt;/span&gt; )&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cout &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&quot;Cheats activated!&quot;&lt;/span&gt; &amp;lt;&amp;lt; endl;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeG&quot;&gt;//Output standard program behavior.&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cout &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&quot;Program finished&quot;&lt;/span&gt; &amp;lt;&amp;lt; endl;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeB&quot;&gt;return&lt;/span&gt; ( 0 );&lt;br /&gt;}&lt;br /&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;&lt;br /&gt;

The above&#39;s output will differ depending on the command line argument passed in. No matter what the program will output the &quot;Program finished&quot; message. If the program is ran with &lt;span id=&quot;codeNRef&quot;&gt;-dev&lt;/span&gt; then it will output &quot;Developer Mode Activated!&quot;. If the program is ran with &lt;span id=&quot;codeNRef&quot;&gt;-cheats&lt;/span&gt; then it will output &quot;Cheats activated!&quot;. If both, &lt;span id=&quot;codeNRef&quot;&gt;-dev&lt;/span&gt; and &lt;span id=&quot;codeNRef&quot;&gt;-cheats&lt;/span&gt;, are added then both of the conditions will occur.&lt;br /&gt;&lt;br /&gt;Now, this program is just an example so it doesn&#39;t test for variations in upper or lowercase nor does it inform the usue when an argument was attempted but matches none of the conditions such as if -DEV, instead of -dev, nothing happens and the user is not informed that -DEV is not a valid parameter.&lt;br /&gt;&lt;br /&gt;Obviously that is a relatively easy fix by saving the argv[ ] to a string and lower-casing it before comparison.&lt;br /&gt;&lt;br /&gt;In addition, the program could be made to output a help message if an argument is attempted but is not one of the conditions by changing the ifs structure to an &lt;span id=&quot;codeBRef&quot;&gt;if else&lt;/span&gt; or a &lt;span id=&quot;codeBRef&quot;&gt;switch&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;h4&gt;
Setting Command Line Arguments&lt;/h4&gt;
&lt;h5&gt;
Adding Argument to A Shortcut&lt;/h5&gt;
Since handling a command line argument is only useful if you know how to run a program with one we should discuss how to add the desired argument. Typically the argument is entered after a space in the command to run a program. This can be done in Windows via a shortcut by right clicking on the shortcut, selecting it&#39;s properties, and appending the argument to the target field.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi0cion5Uiv8IPXeleErIAhk8JW2mhHAchDWzt2xSPzZ-wfUBwzFSz-ftxDJJer8Fa-zZn9nCLRDTGYTD1EKu_DfQ_PiAIXNr-pQD8zF2vxQkLzUNpkoVPmvjAGo4fpL-EqmX1aTPpW4BBu/s1600/shortcutArg.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi0cion5Uiv8IPXeleErIAhk8JW2mhHAchDWzt2xSPzZ-wfUBwzFSz-ftxDJJer8Fa-zZn9nCLRDTGYTD1EKu_DfQ_PiAIXNr-pQD8zF2vxQkLzUNpkoVPmvjAGo4fpL-EqmX1aTPpW4BBu/s320/shortcutArg.png&quot; height=&quot;320&quot; width=&quot;233&quot;&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
So the shortcut&#39;s target field would be the filepath\filename argument. For example, the target field might be: &lt;span id=&quot;codeNRef&quot;&gt;C:\CmdLineArguments\Debug\CmdLineArguments.exe MyArgument&lt;/span&gt; or &lt;span id=&quot;codeNRef&quot;&gt;C:\CmdLineArguments\Debug\CmdLineArguments.exe -h&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;h5&gt;
Adding Argument to Visual Studio&lt;/h5&gt;
It is also useful to add command line arguments to a program that is being actively developed. This will add the special argument each time the program is ran from inside Visual Studio. For Visual Studio 2010, this is done by right-clicking on the Project, selecting properties » Configuration Properties » Debugging » Command Arguments field, where you can enter arguments to be added each run.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEif2PjggKPYW2GPsO6lR0FHbkdGK2NNqlUnVWGxxT_pv7VCKWpdmpF-H7OQKVMCyLXVjQsLMxURpzps3RtlRnMaOBdxYhWl5MHoFAJOpFpriM4l1lGHrkwWh-ZCoHmqO6LY5fdwIdoFvrgU/s1600/IDECmdLine.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEif2PjggKPYW2GPsO6lR0FHbkdGK2NNqlUnVWGxxT_pv7VCKWpdmpF-H7OQKVMCyLXVjQsLMxURpzps3RtlRnMaOBdxYhWl5MHoFAJOpFpriM4l1lGHrkwWh-ZCoHmqO6LY5fdwIdoFvrgU/s320/IDECmdLine.png&quot; height=&quot;180&quot; width=&quot;320&quot;&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://cppmonastery.blogspot.com/feeds/3664999230080805704/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cppmonastery.blogspot.com/2013/01/command-line-arguments.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1543848439127765809/posts/default/3664999230080805704'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1543848439127765809/posts/default/3664999230080805704'/><link rel='alternate' type='text/html' href='http://cppmonastery.blogspot.com/2013/01/command-line-arguments.html' title='Command Line Arguments'/><author><name>TickNSwisted</name><uri>http://www.blogger.com/profile/10882833507254467617</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgAgtxRvfUiDN0kOB9N9gmAOagKqBysZt0mMuAxVoWdTjow8ECKpqWncauHa0dc0vA8qwR-01OJv2SipvzGhr9-ERWD9KcsUhZbSRmcUY2txbGYocQ4gfX-d75hAhQX-yvjxYywtVnXv9o2/s72-c/CmdLineOut1Lrg.png" height="72" width="72"/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1543848439127765809.post-1072929293561850298</id><published>2013-01-18T03:00:00.000-05:00</published><updated>2013-01-24T14:17:52.050-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Beginner"/><category scheme="http://www.blogger.com/atom/ns#" term="C++"/><title type='text'>Random Numbers Using Rand ( )</title><content type='html'>&lt;style&gt;

#codeTable
{
    font-family: Consolas;
    font-style: normal;
    font-weight: normal;
    border:2px solid black;
    border-style: solid;
    border-color: #000000;
    background-color: white;
    text-align: left;
    color: black;
}

#codeOutput
{   
    padding-top:30px;
    padding-bottom:30px;
    padding-right:30px;
    padding-left:30px;
    font-family: &quot;Lucida Console&quot;, Courier New, Serif
    font-style: normal;
    font-weight: normal;
    font-size: 12 px;
    background-color: black;
    color: #cdcdcd;
    border:2px solid #cdcdcd;
    border-style: solid;
    border-color: #cdcdcd;
    background-color: #000000;
    text-align: left;
    color: #cdcdcd;
}

#codeNRef
{
    font-family: Consolas;
    font-style: normal;
    font-weight: normal;
    text-align: left;
    color: white;
}

#codeBRef
{
    font-family: Consolas;
    font-style: normal;
    font-weight: normal;
    text-align: left;
    color: #3399ff;
}

#codeTitle
{
    background-color: dark gray;
    font-family: Arial;
    font-style: italic;
    font-weight: bold;
    font-size: 12px;
    text-align: center;
    color: black;
}

#codeFile
{
    font-family: &quot;Lucida Console&quot;, Courier New, Serif;
    font-style: normal;
    font-weight: normal;
    font-size: 12 px;
    background-color: black;
    text-align:center;
    color: yellow;
}

#codeN
{
    font-family: Consolas;
    font-style: normal;
    font-weight: normal;
    text-align: left;
    color: black;
}

#codeG
{
    font-family: Consolas;
    font-style: normal;
    font-weight: normal;
    text-align: left;
    color: green;
}

#codeR
{
    font-family: Consolas;
    font-style: normal;
    font-weight: normal;
    text-align: left;
    color: #a31515;
}

#codeB
{
    font-family: Consolas;
    font-style: normal;
    font-weight: normal;
    text-align: left;
    color: blue;
}
&lt;/style&gt;
Having the ability to generate random numbers in a program can be very useful, whether it&#39;s to make a simple number guessing game or to see how a program accepts a variety of possible inputs. For our purposes,&amp;nbsp;a random number is a&amp;nbsp;value&amp;nbsp;which&amp;nbsp;was not&amp;nbsp;obvious before creation. And, any number in the availible range should be just as likely as any other, known as a uniform distribution.&lt;br /&gt;&lt;br /&gt;There are at least two ways to accomplish this feat. For exploring the&amp;nbsp;two examples below,&amp;nbsp;I will use the following, &lt;em&gt;The Base Main&lt;/em&gt;. The function body of &lt;span id=&quot;codeNRef&quot;&gt;RandomNumber( )&lt;/span&gt; will be the only difference between the examples. We use the &lt;span id=&quot;codeNRef&quot;&gt;rand( )&lt;/span&gt;&amp;nbsp;function which returns a number which is offset by a random seed. The seed is set by passing the &lt;span id=&quot;codeNRef&quot;&gt;srand( )&lt;/span&gt; function with a number, generally the time.&lt;br /&gt;&lt;br /&gt;
&lt;table id=&quot;codeTable&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;
&lt;td id=&quot;codeTitle&quot;&gt;The Base Main&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td id=&quot;codeFile&quot;&gt;main.cpp&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td id=&quot;CodeTable&quot;&gt;&lt;span id=&quot;codeG&quot;&gt;//===================================INCLUDES==================================&lt;/span&gt;&lt;br /&gt;&lt;span id=&quot;codeB&quot;&gt;#include&lt;/span&gt; &lt;span id=&quot;codeR&quot;&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span id=&quot;codeG&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //Included 
for text output, cout.&lt;/span&gt;&lt;br /&gt;&lt;span id=&quot;codeB&quot;&gt;#include&lt;/span&gt; &lt;span id=&quot;codeR&quot;&gt;&amp;lt;time.h&amp;gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//Included to get current time.&lt;br /&gt;&lt;br /&gt;//Assume namespace 
::std.&lt;/span&gt;&lt;br /&gt;&lt;span id=&quot;codeB&quot;&gt;using namespace&lt;/span&gt; std;&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;span id=&quot;codeG&quot;&gt;//=============================FUNCTION&amp;nbsp;PROTOTYPES=============================&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; RandomNumber( &lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; min, &lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; max );&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span id=&quot;codeG&quot;&gt;//==================================MAIN&amp;nbsp;ENTRY=================================&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; main( &lt;span id=&quot;codeB&quot;&gt;void&lt;/span&gt; )&lt;br /&gt;
{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//Initialize Rand( ) function&#39;s seed with current time.&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; srand( &lt;span id=&quot;codeB&quot;&gt;static_cast&lt;/span&gt;&amp;lt;&lt;span id=&quot;codeB&quot;&gt;unsigned int&lt;/span&gt;&amp;gt;( time(NULL) ) );&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//Loop for 100 times.&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;for&lt;/span&gt; ( &lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; i = 0; i &amp;lt; 100; i++ )&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//Output a number 
between 1 and 100.&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cout 
&amp;lt;&amp;lt; RandomNumber( 1, 100 ) &amp;lt;&amp;lt; endl;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//End the program, returning 
zero.&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;return&lt;/span&gt; ( 0 );&lt;br /&gt;
}&lt;br /&gt;&lt;br /&gt;&lt;span id=&quot;codeG&quot;&gt;//=============================FUNCTION&amp;nbsp;DEFINITIONS============================&lt;/span&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;The above code seeds the randomization with the system time. When&amp;nbsp;a&amp;nbsp;function definition for &lt;span id=&quot;codeNRef&quot;&gt;RandomNumber( )&lt;/span&gt;&amp;nbsp;is appended,&amp;nbsp;100 numbers are 
generated to the screen.&lt;br /&gt;&lt;br /&gt;
&lt;h4&gt;
The Modulus Method&lt;/h4&gt;Currently the popular implementation for generating pseudo-random numbers in C++, &lt;em&gt;The Modulus Method&lt;/em&gt;, is so widely used that not many people consider the process. Even though we can&#39;t employ actual randomness since most computers behave predictably, there&#39;s no reason we can&#39;t explore some different implimentations.&lt;br /&gt;&lt;br /&gt;
&lt;table id=&quot;codeTable&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;
&lt;td id=&quot;codeTitle&quot;&gt;The Modulus Method&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td id=&quot;codeFile&quot;&gt;main.cpp, RandomNumber( )&lt;/td&gt;
&lt;/tr&gt;&lt;tr class=&quot;code&quot;&gt;&lt;td&gt;&lt;span id=&quot;codeG&quot;&gt;//=============================FUNCTION&amp;nbsp;DEFINITIONS============================&lt;br /&gt;//-----------------------------------------------------------------------------&lt;br /&gt;// Function:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;RandomNumber&lt;br /&gt;// Description: Returns a random number between min value and max value,&lt;br /&gt;//&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;including min and max. [min, ..., max]&lt;br /&gt;//-----------------------------------------------------------------------------&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; RandomNumber(&amp;nbsp;&lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; min,&amp;nbsp;&lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; max )&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeG&quot;&gt;//Return a random number between min and max, inclusive.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// Adds minimum value to the 
remainder of division of the number returned&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// from 
Rand() function and the maximum number, plus one, less the minimum.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeB&quot;&gt;return&lt;/span&gt; ( min + rand( ) % ( (max + 1) - min ) );&lt;br /&gt;}&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
The random number is seeded by passing the time, an &lt;span id=&quot;codeBRef&quot;&gt;unsigned 
integer&lt;/span&gt;,&amp;nbsp;into the &lt;span id=&quot;codeNRef&quot;&gt;srand( )&lt;/span&gt; function. This 
ensures that the numbers returned from calls to &lt;span id=&quot;codeNRef&quot;&gt;rand ( )
&lt;/span&gt; are potentially different each time the program runs.&lt;br /&gt;
&lt;br /&gt;The &lt;span id=&quot;codeNRef&quot;&gt;rand( )&lt;/span&gt; function returns a random integer which is between 0 and the&amp;nbsp;&lt;span id=&quot;codeNRef&quot;&gt;RAND_MAX&lt;/span&gt;, the constant specified by the library,&amp;nbsp;which&amp;nbsp;is guarenteed to&amp;nbsp;at least be&amp;nbsp;32,767.&lt;br /&gt;&lt;br /&gt;To&amp;nbsp;get this&amp;nbsp;number within a desired range, other than 0 to 32,767,&amp;nbsp;the &lt;em&gt;modulus operator&lt;/em&gt;, %, is used to calculate the remainder of division&amp;nbsp;between the number returned and the maximum value.&amp;nbsp;A random number between 0 and 9,&amp;nbsp; would use&amp;nbsp;&lt;span id=&quot;codeNRef&quot;&gt;rand( ) % 10&lt;/span&gt;. Any number returned by &lt;span id=&quot;codeNRef&quot;&gt;rand ( )&lt;/span&gt; is&amp;nbsp;less than 10.&amp;nbsp;For example, if &lt;span id=&quot;codeNRef&quot;&gt;rand( )&lt;/span&gt; returns 8000,&amp;nbsp;the modulus operation would divide&amp;nbsp;8000 / 10, which&amp;nbsp;evenly divides, so&amp;nbsp;there would be a&amp;nbsp;remainder of 0, which would be our random number result for that run.&lt;br /&gt;&lt;br /&gt;In the RandomNumber function&amp;nbsp;the following is used to&amp;nbsp;specify the minimum and maximum range for the resulting random number: &lt;br /&gt;

&lt;table style=&quot;background-color: white; border: 1px solid gray; color: black; font-family: Consolas;&quot;&gt;&lt;tbody style=&quot;background-color: white; border: 1px solid gray; color: black; font-family: Consolas;&quot;&gt;
&lt;tr&gt;&lt;td&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;( min + rand( ) % ( (&lt;strong&gt;max + 1&lt;/strong&gt;) - min ) );&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
More simply, whatever the random number is, is then acted on by the modulus to clamp its upper bounds and then has the minimum value added to it to clamp its lower bounds. There&#39;s some repetition, albeit sporatic, to the remainder of numbers early on in the number system which isn&#39;t so in later numbers, which could sometimes makes the randomness less uniform than desired.&lt;br /&gt;&lt;br /&gt;
&lt;h5&gt;Modulus Step-by-step&lt;/h5&gt;To show how this works, consider if the minimum of 1 and maximum of&amp;nbsp;10 used.&lt;br /&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;RandomNumber(1, 10);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The maximum desired number is increased by one to allow for the maximum number itself to possibly result.&lt;br /&gt;
&lt;table style=&quot;background-color: white; border: 1px solid gray; color: black; font-family: Consolas;&quot;&gt;&lt;tbody style=&quot;background-color: white; border: 1px solid gray; color: black; font-family: Consolas;&quot;&gt;&lt;tr&gt;&lt;td&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;(&amp;nbsp;1 + rand( ) % ( &lt;strong&gt;(10 + 1)&lt;/strong&gt; -&amp;nbsp;1 ) );&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;The result, 11, is minused from the minimum desired number, in this case 1. This makes the number to the right of the modulus the equivalent of the desired range from zero. &lt;br /&gt;&lt;table style=&quot;background-color: white; border: 1px solid gray; color: black; font-family: Consolas;&quot;&gt;&lt;tbody style=&quot;background-color: white; border: 1px solid gray; color: black; font-family: Consolas;&quot;&gt;
&lt;tr&gt;&lt;td&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;(&amp;nbsp;1 + rand( ) % &lt;strong&gt;(&amp;nbsp;11 -&amp;nbsp;1 )&lt;/strong&gt; );&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;Rand ( ) is ultimately a number, it is the driving force in getting a different result each time, so let&#39;s say that rand ( ) returns 4892, just a random number between 0 and &lt;span id=&quot;codeNRef&quot;&gt;RAND_MAX&lt;/span&gt;.&lt;br /&gt;&lt;table style=&quot;background-color: white; border: 1px solid gray; color: black; font-family: Consolas;&quot;&gt;&lt;tbody style=&quot;background-color: white; border: 1px solid gray; color: black; font-family: Consolas;&quot;&gt;&lt;tr&gt;&lt;td&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;(&amp;nbsp;1 + &lt;strong&gt;rand( ) %&amp;nbsp;10&lt;/strong&gt; );&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;The&amp;nbsp;result of&amp;nbsp;4892 % 10 is 2.&lt;br /&gt;&lt;table style=&quot;background-color: white; border: 1px solid gray; color: black; font-family: Consolas;&quot;&gt;&lt;tbody style=&quot;background-color: white; border: 1px solid gray; color: black; font-family: Consolas;&quot;&gt;&lt;tr&gt;&lt;td&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;(&amp;nbsp;1 +&amp;nbsp;&lt;strong&gt;4892 % 10&lt;/strong&gt; );&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;The minimum desired value, 1, is then added to the resulting remainder, 2, to ensure it is at least the minimum value. This guarentees that the number will never be the same or higher than the number  divided by. This should make sense, if you divide by a number you&#39;ll never get a remainder of that number.&lt;br /&gt;&lt;table style=&quot;background-color: white; border: 1px solid gray; color: black; font-family: Consolas;&quot;&gt;&lt;tbody style=&quot;background-color: white; border: 1px solid gray; color: black; font-family: Consolas;&quot;&gt;
&lt;tr&gt;&lt;td&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;( &lt;strong&gt;1&lt;/strong&gt; +&amp;nbsp;&lt;strong&gt;2&lt;/strong&gt; );&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;The number returned by calling &lt;span id=&quot;codeNRef&quot;&gt;RandomNumber(1, 10)&lt;/span&gt; in this case is 3.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;The Do-While Method&lt;/h3&gt;Rather than using the somewhat limited remainder of division and then clamping down on whatever number results, we could instead call random until a number within our desired range comes about.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;table id=&quot;codeTable&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;
&lt;td id=&quot;codeTitle&quot;&gt;The Do-While Method&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td id=&quot;codeFile&quot;&gt;main.cpp&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td id=&quot;codeTable&quot;&gt;&lt;span id=&quot;codeG&quot;&gt;//=============================FUNCTION&amp;nbsp;DEFINITIONS============================&lt;br /&gt;//-----------------------------------------------------------------------------&lt;br /&gt;// Function:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;RandomNumber&lt;br /&gt;// Description: Returns a random number between min value and max value,&lt;br /&gt;//&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;including min and max. [min, ..., max]&lt;br /&gt;//-----------------------------------------------------------------------------&lt;/span&gt;&lt;br /&gt;&lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; RandomNumber ( &lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; min, &lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; max )&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; randomNumber;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeB&quot;&gt;do&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeG&quot;&gt;//Set 
randomNumber equal to a random number.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;randomNumber = rand ( );&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeG&quot;&gt;//Generate a new random number while randomNumber&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// is less than the minimum or more than 
maximum.&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeB&quot;&gt;while&lt;/span&gt; ( 
randomNumber &amp;lt; min || randomNumber &amp;gt; max );&lt;br /&gt;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeG&quot;&gt;//Return the random number between, and including, min and max.&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeB&quot;&gt;return&lt;/span&gt; randomNumber;&lt;br /&gt;}&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;In this code we create a local variable, &lt;span id=&quot;codeBRef&quot;&gt;int&lt;/span&gt; &lt;span id=&quot;codeNRef&quot;&gt;randomNumber&lt;/span&gt;, to hold the different random numbers produced 
by &lt;span id=&quot;codeNRef&quot;&gt;rand( )&lt;/span&gt;, which is some amount between 0 and &lt;span id=&quot;codeNRef&quot;&gt;RAND_MAX&lt;/span&gt;. Should the &lt;span id=&quot;codeNRef&quot;&gt;randomNumber&lt;/span&gt; 
generated by &lt;span id=&quot;codeNRef&quot;&gt;rand( )&lt;/span&gt; be outside the range, &lt;span id=&quot;codeNRef&quot;&gt;randomNumber &amp;lt; min || randomNumber &amp;gt; max&lt;/span&gt;, we&amp;nbsp;&lt;span id=&quot;codeBRef&quot;&gt;do&lt;/span&gt;&amp;nbsp;it again until it is within our desired 
range.&amp;nbsp;Essentially, it simply generates a random number until it gets one 
within the range between &lt;span id=&quot;codeNRef&quot;&gt;min&lt;/span&gt; and &lt;span id=&quot;codeNRef&quot;&gt;max&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;The distribution is more uniform than the 
modulus method above, but the smaller the range the more time it takes to return a 
number. This is something to consider if the program is very processor intensive 
where millions of time-sensitive operations occur every second.&lt;br /&gt;
&lt;br /&gt;&lt;h3&gt;Random Number Generation Frequency&lt;/h3&gt;You might wonder if the distribution pattern actually skewed. In some cases, it is possible that certain numbers appear more frequently, though it&#39;s not apocalyptic, and users wont be able to predict with certainty the next number to be generated. However, to give you an idea of the distribution pattern for both methods, I&#39;ll use the code below to do a side-by-side comparison:&lt;br /&gt;&lt;br /&gt;&lt;table id=&quot;codeTable&quot;&gt;&lt;tbody&gt;&lt;tr&gt;
&lt;td id=&quot;codeTitle&quot;&gt;Generating a Frequency Report&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td id=&quot;codeFile&quot;&gt;main.cpp&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td id=&quot;CodeTable&quot;&gt;&lt;span id=&quot;codeG&quot;&gt;//===================================INCLUDES==================================&lt;/span&gt;&lt;br /&gt;&lt;span id=&quot;codeB&quot;&gt;#include&lt;/span&gt;&amp;nbsp;&lt;span id=&quot;codeR&quot;&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeG&quot;&gt;//Included for text output, cout.&lt;/span&gt;&lt;br /&gt;&lt;span id=&quot;codeB&quot;&gt;#include&lt;/span&gt;&amp;nbsp;&lt;span id=&quot;codeR&quot;&gt;&amp;lt;iomanip&amp;gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeG&quot;&gt;//Included for output formatting manipulation.&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;#include&lt;/span&gt;&amp;nbsp;&lt;span id=&quot;codeR&quot;&gt;&amp;lt;time.h&amp;gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeG&quot;&gt;//Included to get current time.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;&lt;span id=&quot;codeG&quot;&gt;//Assuming namespace ::std.&lt;/span&gt;&lt;br /&gt;&lt;span id=&quot;codeB&quot;&gt;using namespace&lt;/span&gt; std;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span id=&quot;codeG&quot;&gt;//=============================FUNCTION&amp;nbsp;PROTOTYPES=============================&lt;/span&gt;&lt;br /&gt;&lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; RandomNumber_Modulus( &lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; min, &lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; max );&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; RandomNumber_DoWhile( &lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; min, &lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; max );&lt;br /&gt;&lt;br /&gt;&lt;span id=&quot;codeG&quot;&gt;//==================================MAIN&amp;nbsp;ENTRY=================================&lt;/span&gt;&lt;br /&gt;&lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; main( &lt;span id=&quot;codeB&quot;&gt;void&lt;/span&gt; )&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeB&quot;&gt;const int&lt;/span&gt; RANGE_MINIMUM = 0;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeB&quot;&gt;const int&lt;/span&gt; RANGE_MAXIMUM = 50;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeB&quot;&gt;const int&lt;/span&gt; SAMPLE_SIZE = 9000;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeG&quot;&gt;//Declare frequency variables to zero.&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt;modulusFrequency[RANGE_MAXIMUM - RANGE_MINIMUM] = {0};&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; dowhileFrequency[RANGE_MAXIMUM - RANGE_MINIMUM] = {0};&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; modulusResult = 0;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; dowhileResult = 0;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeB&quot;&gt;//Initialize Rand( ) function&#39;s seed with current time.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;srand( &lt;span id=&quot;codeB&quot;&gt;static_cast&lt;/span&gt;&amp;lt;&lt;span id=&quot;codeB&quot;&gt;unsigned int&lt;/span&gt;&amp;gt;( time (NULL) ) );&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeG&quot;&gt;//Generate 
SAMPLE_SIZE count of random numbers per generation method.&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeB&quot;&gt;for&lt;/span&gt; ( &lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; i = 0; i &amp;lt; SAMPLE_SIZE; i++ )&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeG&quot;&gt;//Set result variable to the random number from respective function.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;modulusResult = RandomNumber_Modulus( RANGE_MINIMUM, RANGE_MAXIMUM );&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;dowhileResult = RandomNumber_DoWhile( RANGE_MINIMUM, RANGE_MAXIMUM );&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeG&quot;&gt;//Increment frequency count for the number returned.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;modulusFrequency[modulusResult]+
+;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;dowhileFrequency
[dowhileResult]++;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeG&quot;&gt;//Output a table header&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cout &amp;lt;&amp;lt; left&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;&amp;lt; setw(16) &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&quot;RANGE_MINIMUM :&quot;&lt;/span&gt; &amp;lt;&amp;lt; setw(15) &amp;lt;&amp;lt; 
RANGE_MINIMUM&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;&amp;lt; 
setw(16) &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&quot;SAMPLE_SIZE   :&quot;&lt;/span&gt; &amp;lt;&amp;lt; setw(15) 
&amp;lt;&amp;lt; SAMPLE_SIZE &amp;lt;&amp;lt; endl&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&amp;lt; setw(16) &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&quot;RANGE_MAXIMUM :&quot;&lt;/span&gt; &amp;lt;&amp;lt; setw(15) &amp;lt;&amp;lt; RANGE_MAXIMUM&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&amp;lt; setw(16) &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&quot;RANGE COUNT :&quot;&lt;/span&gt; &amp;lt;&amp;lt; setw(15) &amp;lt;&amp;lt; 
RANGE_MAXIMUM&amp;nbsp;-&amp;nbsp;RANGE_MINIMUM&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; endl&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&amp;lt; setw(18) &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&quot;THE MODULUS METHOD&quot;&lt;/span&gt; &amp;lt;&amp;lt; setw(13) &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;lt;&amp;lt; setw(19) &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&quot;THE DO-WHILE METHOD&quot;&lt;/span&gt; &amp;lt;&amp;lt; 
endl;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeG&quot;&gt;//Output the frequency 
results.&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeB&quot;&gt;for&lt;/span&gt; ( &lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; i = 0; i &amp;lt; (RANGE_MAXIMUM - RANGE_MINIMUM); i++ )&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//Output the modulus results.&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cout &amp;lt;&amp;lt; setw(3) &amp;lt;&amp;lt; i &amp;lt;&amp;lt; setw(9) &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&quot;occurred&quot;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;&amp;lt; setw(4) &amp;lt;&amp;lt; modulusFrequency[i] &amp;lt;&amp;lt; setw(7) &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&quot;times.&quot;&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; setw(8) &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&quot;&quot;&lt;/span&gt;;&lt;br /&gt;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//Output the do-while results.&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cout &amp;lt;&amp;lt; setw(3) &amp;lt;&amp;lt; i &amp;lt;&amp;lt; setw(9) &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&quot;occurred&quot;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;&amp;lt; setw(4) &amp;lt;&amp;lt; dowhileFrequency[i] &amp;lt;&amp;lt; setw(7) &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&quot;times.&quot;&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; endl;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeG&quot;&gt;//End the program, returning zero.&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeB&quot;&gt;return&lt;/span&gt; ( 0 );&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span id=&quot;codeG&quot;&gt;//=============================FUNCTION&amp;nbsp;DEFINITIONS============================&lt;br /&gt;//-----------------------------------------------------------------------------&lt;br /&gt;// Function:&amp;nbsp;&amp;nbsp;&amp;nbsp; RandomNumber_Modulus&lt;br /&gt;// Description: Returns a random number between min value and max value,&lt;br /&gt;//&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; including min and max. [min, ..., max]&lt;br /&gt;//-----------------------------------------------------------------------------&lt;/span&gt;&lt;br /&gt;&lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; RandomNumber_Modulus( &lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; min, &lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; max )&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//Return a random number between min and max, inclusive.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Adds minimum value to the remainder of division of the number returned&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // from Rand() function 
and the maximum number, plus one, less the minimum.&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;return&lt;/span&gt; ( min + rand( ) % ( (max + 1) - min ) );&lt;br /&gt;}&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;&lt;span id=&quot;codeG&quot;&gt;//-----------------------------------------------------------------------------&lt;br /&gt;// Function:&amp;nbsp;&amp;nbsp;&amp;nbsp; RandomNumber_DoWhile&lt;br /&gt;// Description:&amp;nbsp;Returns a random number between min value and max value,&lt;br /&gt;//&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; including min and max. [min, ..., max]&lt;br /&gt;//-----------------------------------------------------------------------------&lt;/span&gt;&lt;br /&gt;&lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; RandomNumber_DoWhile( &lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; min, &lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; max )&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; randomNumber;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;do&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//Set randomNumber equal to a random number.&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; randomNumber = rand ( );&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//Generate a new random number while randomNumber&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // is less than the minimum or more than maximum.&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;while&lt;/span&gt; ( randomNumber &amp;lt; min || randomNumber &amp;gt; max );&lt;br /&gt;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//Return a random number between min and max, inclusive.&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;return&lt;/span&gt; randomNumber;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;This code creates random &lt;span id=&quot;codeNRef&quot;&gt;SAMPLE_SIZE&lt;/span&gt; count of numbers between &lt;span id=&quot;codeNRef&quot;&gt;RANGE_MINIMUM&lt;/span&gt; and &lt;span id=&quot;codeNRef&quot;&gt;RANGE_MAXIMUM&lt;/span&gt; using the modulus method, &lt;span id=&quot;codeNRef&quot;&gt;RandomNumber_Modulus( )&lt;/span&gt;, and the do-while method, &lt;span id=&quot;codeNRef&quot;&gt;RandomNumber_DoWhile( )&lt;/span&gt;. The modulus random number result, &lt;span id=&quot;codeNRef&quot;&gt;modulusResult&lt;/span&gt;, is added to the element&amp;nbsp;of the modulus&amp;nbsp;frequency array, &lt;span id=&quot;codeNRef&quot;&gt;modulusFrequency&lt;/span&gt;. The do-while&amp;nbsp;random number, &lt;span id=&quot;codeNRef&quot;&gt;dowhileResult&lt;/span&gt;, is added to the&amp;nbsp;element &lt;span id=&quot;codeNRef&quot;&gt;dowhileFrequency&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;Then the program outputs to the screen, the count of each number&amp;nbsp;that occurred for&amp;nbsp;each method. This is a small range, but&amp;nbsp;the purpose of a uniform number generator is to have all numbers within the range&amp;nbsp;equally possible; there shouldn&#39;t be any one number&amp;nbsp;occuring&amp;nbsp;too&amp;nbsp;often compared to the others.&lt;br /&gt;&lt;br /&gt;Here&#39;s a sample output:&lt;br /&gt;&lt;div class=&quot;separator&quot; style=&quot;align: center; clear: both;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiU2YfBw4fzjDac744P1bGP9j73XRuQivjfoDp2U-HqC_gxN5RSTSLijTsg4j0XBGEssbHW3QWHupKFJoYvBIXrkL_p0HY4_on43Y3aQYdd7ieENakkT0PH_4w_uzX3JXOAP-XlgfXjzU8j/s1600/randomNumberFrequency.png&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiU2YfBw4fzjDac744P1bGP9j73XRuQivjfoDp2U-HqC_gxN5RSTSLijTsg4j0XBGEssbHW3QWHupKFJoYvBIXrkL_p0HY4_on43Y3aQYdd7ieENakkT0PH_4w_uzX3JXOAP-XlgfXjzU8j/s400/randomNumberFrequency.png&quot; height=&quot;400&quot; width=&quot;385&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://cppmonastery.blogspot.com/feeds/1072929293561850298/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cppmonastery.blogspot.com/2013/01/random-numbers-using-rand.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1543848439127765809/posts/default/1072929293561850298'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1543848439127765809/posts/default/1072929293561850298'/><link rel='alternate' type='text/html' href='http://cppmonastery.blogspot.com/2013/01/random-numbers-using-rand.html' title='Random Numbers Using Rand ( )'/><author><name>TickNSwisted</name><uri>http://www.blogger.com/profile/10882833507254467617</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiU2YfBw4fzjDac744P1bGP9j73XRuQivjfoDp2U-HqC_gxN5RSTSLijTsg4j0XBGEssbHW3QWHupKFJoYvBIXrkL_p0HY4_on43Y3aQYdd7ieENakkT0PH_4w_uzX3JXOAP-XlgfXjzU8j/s72-c/randomNumberFrequency.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1543848439127765809.post-5599643565393709148</id><published>2013-01-11T03:00:00.000-05:00</published><updated>2013-01-21T11:11:04.818-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Beginner"/><category scheme="http://www.blogger.com/atom/ns#" term="C++"/><category scheme="http://www.blogger.com/atom/ns#" term="Console Window"/><title type='text'>Dividing and Centering Text Output</title><content type='html'>&lt;style&gt;

#codeTable
{
    font-family: Consolas;
    font-style: normal;
    font-weight: normal;
    border:2px solid black;
    border-style: solid;
    border-color: #000000;
    background-color: white;
    text-align: left;
    color: black;

}

#codeOutput
{   
    padding-top:30px;
    padding-bottom:30px;
    padding-right:30px;
    padding-left:30px;
    font-family: &quot;Lucida Console&quot;, Courier New, Serif
    font-style: normal;
    font-weight: normal;
    font-size: 12 px;
    background-color: black;
    color: #cdcdcd;
    border:1px solid #cdcdcd;
    border-style: solid;
    border-color: #cdcdcd;
    background-color: #000000;
    text-align: left;
    color: #cdcdcd;
}


#codeNRef
{
    font-family: Consolas;
    font-style: normal;
    font-weight: normal;
    text-align: left;
    color: white;
}

#codeTitle
{
    background-color: dark gray;
    font-family: Arial;
    font-style: italic;
    font-weight: bold;
    font-size: 12px;
    text-align: center;
    color: black;
}

#codeFile
{
    font-family: &quot;Lucida Console&quot;, Courier New, Serif
    font-style: normal;
    font-weight: normal;
    font-size: 12 px;
    background-color: black;
    text-align:center;
    color: yellow;
}


#codeN
{
    font-family: Consolas;
    font-style: normal;
    font-weight: normal;
    text-align: left;
    color: black;
}

#codeG
{
    font-family: Consolas;
    font-style: normal;
    font-weight: normal;
    text-align: left;
    color: green;
}

#codeR
{
    font-family: Consolas;
    font-style: normal;
    font-weight: normal;
    text-align: left;
    color: #a31515;
}

#codeB
{
    font-family: Consolas;
    font-style: normal;
    font-weight: normal;
    text-align: left;
    color: blue;
}
&lt;/style&gt;

&lt;br /&gt;
&lt;h2&gt;
Dividing and Centering Text Output&lt;/h2&gt;
&lt;br /&gt;
Sometimes we need text output to the console window or a file to be centered and or preceeded by a divider to make it more readable. In such cases, we can create two functions &lt;span id=&quot;codeNRef&quot;&gt;OutputDivider ( )&lt;/span&gt; and &lt;span id=&quot;codeNRef&quot;&gt;OutputCentered ( )&lt;/span&gt; to add to our formatting ability. A complete program example at the end shows how to use both functions together.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
The OutputDivider Function:&lt;/h3&gt;
The divider function should accept a stream to output. The stream could be a file stream, ifstream, or output to the screen, cout. We should set what character is repeated across the screen and how many times, which is the desired length of the divider.&lt;br /&gt;
&lt;br /&gt;
&lt;table id=&quot;codeTable&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;
&lt;td id=&quot;codeTitle&quot;&gt;Function Definition:&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td id=&quot;codeFile&quot;&gt;OutputDivider( )&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td id=&quot;codeTable&quot;&gt;&lt;span id=&quot;codeG&quot;&gt;//-----------------------------------------------------------------------------&lt;br /&gt;// Function:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;OutputDivider&lt;br /&gt;// Description:&amp;nbsp;&amp;nbsp;Output fillSymbol character a count of numberOfSymbols to&lt;br /&gt;//&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;streamOut, keeping the original fill character when done.&lt;br /&gt;//-----------------------------------------------------------------------------&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;void&lt;/span&gt; OutputDivider( ostream&amp;amp; outStream, &lt;span id=&quot;codeB&quot;&gt;char&lt;/span&gt; fillSymbol, &lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt;
 numberOfSymbols )&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;char&lt;/span&gt; oldFillSymbol;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//Set oldFillSymbol to the current fill character.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; oldFillSymbol = outStream.fill( );&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//Set the fill to the fillSymbol passed.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; outStream.fill( fillSymbol );&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//Output to stream, outStream, the fill across width, numberOfSymbols.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; outStream &amp;lt;&amp;lt; setw( numberOfSymbols ) &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&quot;&quot;&lt;/span&gt; &amp;lt;&amp;lt; endl;
&lt;br /&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//Restore old fill symbol so not to affect outside setw( ) formatting.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; outStream.fill( oldFillSymbol );&lt;br /&gt;
}
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;

&lt;br /&gt;
This function works by accepting, as the first parameter, an &lt;span id=&quot;codeNRef&quot;&gt;ostream&lt;/span&gt;&amp;nbsp;which all output will be sent to, named &lt;span id=&quot;codeNRef&quot;&gt;outStream&lt;/span&gt;. This might be a type of file stream, such as&amp;nbsp;&lt;span id=&quot;codeNRef&quot;&gt;ofstream&lt;/span&gt;,&amp;nbsp;perhaps named&amp;nbsp;&lt;span id=&quot;codeNRef&quot;&gt;myOutputFile&lt;/span&gt;, or&amp;nbsp;to &lt;span id=&quot;codeNRef&quot;&gt;cout&lt;/span&gt;, the screen. The second parameter passed into the function is &lt;span id=&quot;codeB&quot;&gt;char&lt;/span&gt; &lt;span id=&quot;codeNRef&quot;&gt;fillSymbol&lt;/span&gt;, which is the symbol or character&amp;nbsp;to be output. The third parameter,&amp;nbsp;&lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; &lt;span id=&quot;codeNRef&quot;&gt;numberOfSymbols&lt;/span&gt;, is the number of times the character is repeatedly printed.&lt;br /&gt;
&lt;br /&gt;
Once a fill symbol is set for a &lt;span id=&quot;codeNRef&quot;&gt;stream&lt;/span&gt;, such as with a call to &lt;span id=&quot;codeNRef&quot;&gt;outStream.fill( )&lt;/span&gt; the symbol for that &lt;span id=&quot;codeNRef&quot;&gt;stream&lt;/span&gt; is then changed going forward, whether or not we are in the local scope of a function or not. This is because we are directing changes to the actual &lt;span id=&quot;codeNRef&quot;&gt;stream&lt;/span&gt; itself, through the reference&amp;nbsp;to&amp;nbsp;&lt;span id=&quot;codeNRef&quot;&gt;ostream&amp;amp;&lt;/span&gt; instead of a local copy. To make sure outside calls to &lt;span id=&quot;codeNRef&quot;&gt;setw( )&lt;/span&gt;&amp;nbsp;aren&#39;t changed after each run of our function&amp;nbsp;we store the original fill in &lt;span id=&quot;codeB&quot;&gt;char&lt;/span&gt; &lt;span id=&quot;codeNRef&quot;&gt;oldFillSymbol&lt;/span&gt; to be restored at the end.&lt;br /&gt;
&lt;br /&gt;
Then we set the fill to &lt;span id=&quot;codeNRef&quot;&gt;fillSymbol&lt;/span&gt; passed and subsequently output the fill across the screen using &lt;span id=&quot;codeNRef&quot;&gt;setw( )&lt;/span&gt;.&amp;nbsp;Here&#39;s how the function is called, see the final example at the end for further clarification:&lt;br /&gt;&lt;br /&gt;
&lt;table id=&quot;codeTable&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;
&lt;td id=&quot;codeTitle&quot;&gt;Function Call:&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td id=&quot;codeFile&quot;&gt;OutputDivider( )&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td id=&quot;codeTable&quot;&gt;&lt;span id=&quot;codeG&quot;&gt;//Output to cout a divider of dashes, &#39;-&#39;, across the screen, 50 chars.&lt;/span&gt;&lt;br /&gt;
OutputDivider ( cout, &lt;span id=&quot;codeR&quot;&gt;&#39;-&#39;&lt;/span&gt;
, 50 );
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
The OutputCentered Function:&lt;/h3&gt;
The &lt;span id=&quot;codeNRef&quot;&gt;OutputCentered ( )&lt;/span&gt; function should accept a stream, similar to the &lt;span id=&quot;codeNRef&quot;&gt;OutputDivider()&lt;/span&gt;&amp;nbsp;function above. The output will be of&amp;nbsp;some text, a string, in the center of the resulting output.&lt;br /&gt;
&lt;table id=&quot;codeTable&quot;&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td id=&quot;codeTitle&quot;&gt;Function Definition&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td id=&quot;codeFile&quot;&gt;OutputCentered( )&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td id=&quot;codeTable&quot;&gt;&lt;span id=&quot;codeG&quot;&gt;//-----------------------------------------------------------------------------&lt;br /&gt;// Function:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;OutputCentered&lt;br /&gt;// Description: Outputs to output stream passed, outStream, the string of text&lt;br /&gt;//&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;passed, centerText, across the desired number of characters,&lt;br /&gt;//&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; screenLength.&lt;br /&gt;//-----------------------------------------------------------------------------&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;void&lt;/span&gt; OutputCentered( ostream&amp;amp; outStream, string centerText, &lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; screenLength )&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; centeredLength;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&lt;span id=&quot;codeG&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//Minus the length of the text from the output&#39;s total length, screenWidth.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; centeredLength = screenLength - centerText.length( );&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeG&quot;&gt;//Test if centering is necessary, the centeredLength is two or more.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;if&lt;/span&gt; (centeredLength &amp;gt;= 2)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeG&quot;&gt;//Calculate total width of output to be appropriately centered.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; centeredLength = ( (centeredLength / 2) + centerText.length( ) );&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//Output centerText, string passed, across centeredLength.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; outStream &amp;lt;&amp;lt; setw( centeredLength ) &amp;lt;&amp;lt; centerText &amp;lt;&amp;lt; endl;&lt;br /&gt;
}&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;br /&gt;
&lt;br /&gt;
This function works by accepting, as the first parameter, an &lt;span id=&quot;codeNRef&quot;&gt;ostream&lt;/span&gt; which all output will be sent to, named &lt;span id=&quot;codeNRef&quot;&gt;outStream&lt;/span&gt;. Again, this might be a type of file stream, such as an&amp;nbsp;&lt;span id=&quot;codeNRef&quot;&gt;ofstream&lt;/span&gt; or just an output to screen, &lt;span id=&quot;codeNRef&quot;&gt;cout&lt;/span&gt;. The second and third&amp;nbsp;parameter,&amp;nbsp;a &lt;span id=&quot;codeNRef&quot;&gt;string centerText&lt;/span&gt;&amp;nbsp;and then&amp;nbsp;&lt;span id=&quot;codeNRef&quot;&gt;int&lt;/span&gt; &lt;span id=&quot;codeNRef&quot;&gt;screenLength&lt;/span&gt; which specifies the length of characters to center the&amp;nbsp;text across.&lt;br /&gt;
&lt;br /&gt;
To understand how this works consider&amp;nbsp;&lt;span id=&quot;codeNRef&quot;&gt;screenLength&lt;/span&gt; value represents the number of&amp;nbsp;boxes lined up in a row. We need to put&amp;nbsp;the characters of &lt;span id=&quot;codeNRef&quot;&gt;string centerText&lt;/span&gt; in the center-most boxes. In order to know which box is destined to be an empty box, or how many boxes to keep empty as we move from left to right we start by&amp;nbsp;finding out how many empty boxes we&#39;ll actually have. To do that we&amp;nbsp;minus the length of the text from the total screen size.&amp;nbsp;The second line of the function does this exactly, determines the number of empty boxes (characters).&lt;br /&gt;
&lt;br /&gt;
Next, we test if we need to center at all. If we know there are&amp;nbsp;two or more empty boxes than there are characters the&amp;nbsp;we should do some centering, otherwise there will be no even number of&amp;nbsp;empty boxes so there&#39;s no centering possible and the function should&amp;nbsp;just&amp;nbsp;output &lt;span id=&quot;codeNRef&quot;&gt;string centerText&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
If there is centering to be done we calculate the length to center across, dividing the number of empty characters, &lt;span id=&quot;codeNRef&quot;&gt;centeredWidth&lt;/span&gt; and adding in the length of the string.&lt;br /&gt;
&lt;br /&gt;
Finally, the function&amp;nbsp;outputs the &lt;span id=&quot;codeNRef&quot;&gt;centerText&lt;/span&gt;, which is centered using &lt;span id=&quot;codeNRef&quot;&gt;setw( )&lt;/span&gt;&amp;nbsp;to set the width/length&amp;nbsp;of the&amp;nbsp;calculated &lt;span id=&quot;codeNRef&quot;&gt;centeredLength&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;table id=&quot;codeTable&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;
&lt;td id=&quot;codeTitle&quot;&gt;Function Call:&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td id=&quot;codeFile&quot;&gt;OutputCentered( )&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td id=&quot;codeTable&quot;&gt;&lt;span id=&quot;codeG&quot;&gt;//Output the text, &quot;Centered!&quot;, to cout centered across fifty characters.&lt;/span&gt;&lt;br /&gt;
OutputCentered ( cout, &lt;span id=&quot;codeR&quot;&gt;&quot;Centered!&quot;&lt;/span&gt;, 50 );&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
The Complete Example&lt;/h3&gt;
To wrap up, we can observe both&amp;nbsp;the OutputCentered and Output functions working side by side to&amp;nbsp; tidy up&amp;nbsp;output.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;table id=&quot;codeTable&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;
&lt;td id=&quot;codeTitle&quot;&gt;Complete Example, using OutputCentered and OutputDivider&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td id=&quot;codeFile&quot;&gt;main.cpp&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td id=&quot;codeTable&quot;&gt;&lt;span id=&quot;codeG&quot;&gt;//===================================INCLUDES==================================&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;#include &lt;/span&gt;&lt;span id=&quot;codeR&quot;&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;  &lt;span id=&quot;codeG&quot;&gt;//Included for text output, cout.&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;#include&lt;/span&gt; &lt;span id=&quot;codeR&quot;&gt;&amp;lt;iomanip&amp;gt;&lt;/span&gt; &lt;span id=&quot;codeG&quot;&gt; //Included for setw(), fill(), output manipulation.&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;#include &lt;/span&gt;&lt;span id=&quot;codeR&quot;&gt;&amp;lt;string&amp;gt;&lt;/span&gt;&lt;span id=&quot;codeG&quot;&gt; //Included for string type.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span id=&quot;codeG&quot;&gt;//Assuming namespace ::std.&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;using namespace&lt;/span&gt; std;&lt;br /&gt;
&lt;br /&gt;
&lt;span id=&quot;codeG&quot;&gt;//=============================FUNCTION PROTOTYPES=============================&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;void&lt;/span&gt; OutputDivider( ostream&amp;amp; outStream, &lt;span id=&quot;codeB&quot;&gt;char&lt;/span&gt; fillSymbol, &lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; numberOfSymbols );&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;void&lt;/span&gt; OutputCentered( ostream&amp;amp; outStream, string centerText, &lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; screenLength );&lt;br /&gt;
&lt;br /&gt;
&lt;span id=&quot;codeG&quot;&gt;//==================================MAIN ENTRY=================================&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; main( &lt;span id=&quot;codeB&quot;&gt;void&lt;/span&gt; )&lt;br /&gt;
{&lt;br /&gt;
&lt;span id=&quot;codeG&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //The length of the output, used to set all lengths the same.&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;colorB&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int&lt;/span&gt; outputLength = 60;&lt;br /&gt;
&lt;br /&gt;
&lt;span id=&quot;codeG&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //A string to output, centered, at some point.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; string str1 = &lt;span id=&quot;codeR&quot;&gt;&quot;Neato, n Stuff&quot;&lt;/span&gt;;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span id=&quot;codeG&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //Output a divider of outputLength number of characters.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; OutputDivider ( cout, &lt;span id=&quot;codeR&quot;&gt;&#39;=&#39;&lt;/span&gt;, outputLength );&lt;br /&gt;
&lt;br /&gt;
&lt;span id=&quot;codeG&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //Output &quot;Centered!&quot; centered across outputLength number of characters.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; OutputCentered ( cout, &lt;span id=&quot;codeR&quot;&gt;&quot;Centered!&quot;&lt;/span&gt;, outputLength );&lt;br /&gt;
&lt;br /&gt;
&lt;span id=&quot;codeG&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //Output a divider of outputLength number of characters.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; OutputDivider ( cout, &lt;span id=&quot;codeR&quot;&gt;&#39;*&#39;&lt;/span&gt;, outputLength );&lt;br /&gt;
&lt;br /&gt;
&lt;span id=&quot;codeG&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //Output the string, str1, centered on the screen.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; OutputCentered ( cout, str1, outputLength );&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span id=&quot;codeG&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //End the program, returning zero.&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return&lt;/span&gt; ( 0 );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span id=&quot;codeG&quot;&gt;//=============================FUNCTION DEFINITIONS============================&lt;br /&gt;//-----------------------------------------------------------------------------&lt;br /&gt;// Function:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;OutputDivider&lt;br /&gt;// Description:&amp;nbsp;&amp;nbsp;Output fillSymbol character a count of numberOfSymbols to&lt;br /&gt;//&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;streamOut, keeping the original fill character when done.&lt;br /&gt;//-----------------------------------------------------------------------------&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;void&lt;/span&gt; OutputDivider( ostream&amp;amp; outStream, &lt;span id=&quot;codeB&quot;&gt;char&lt;/span&gt; fillSymbol, &lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt;
 numberOfSymbols )&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;char&lt;/span&gt; oldFillSymbol;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//Set oldFillSymbol to the current fill character.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; oldFillSymbol = outStream.fill( );&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//Set the fill to the fillSymbol passed.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; outStream.fill( fillSymbol );&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//Output to stream, outStream, the fill across width, numberOfSymbols.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; outStream &amp;lt;&amp;lt; setw( numberOfSymbols ) &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&quot;&quot;&lt;/span&gt; &amp;lt;&amp;lt; endl;

&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//Restore old fill symbol so not to affect outside setw( ) formatting.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; outStream.fill( oldFillSymbol );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span id=&quot;codeG&quot;&gt;//-----------------------------------------------------------------------------&lt;br /&gt;// Function:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;OutputCentered&lt;br /&gt;// Description: Outputs to output stream passed, outStream, the string of text&lt;br /&gt;//&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;passed, centerText, across the desired number of characters,&lt;br /&gt;//&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; screenLength.&lt;br /&gt;//-----------------------------------------------------------------------------&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;void&lt;/span&gt; OutputCentered( ostream&amp;amp; outStream, string centerText, &lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; screenLength )&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; centeredLength;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&lt;span id=&quot;codeG&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//Minus the length of the text from the output&#39;s total length, screenWidth.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; centeredLength = screenLength - centerText.length( );&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeG&quot;&gt;//Test if centering is necessary, the centeredLength is two or more.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;if&lt;/span&gt; (centeredLength &amp;gt;= 2)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeG&quot;&gt;//Calculate total width of output to be appropriately centered.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; centeredLength = ( (centeredLength / 2) + centerText.length( ) );&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//Output centerText, string passed, across centeredLength.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; outStream &amp;lt;&amp;lt; setw( centeredLength ) &amp;lt;&amp;lt; centerText &amp;lt;&amp;lt; endl;&lt;br /&gt;
}&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;br /&gt;

&lt;table id=&quot;codeOutput&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Output:&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td id=&quot;codeOutput&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3WAnJDyhw3o1E_LsvJXX_ZEqL1nSHm_46Jg9SEh01QaNRCIVYw0Aibzn9EVN8umFL6osu3VzCbywSute0A_ejIYgQgmYwh3Vp9z7lxQbvCslPeNJ3jt98F3hczWdP2nq5B2vg4kcctyjd/s1600/OutputDivAndCent.png&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3WAnJDyhw3o1E_LsvJXX_ZEqL1nSHm_46Jg9SEh01QaNRCIVYw0Aibzn9EVN8umFL6osu3VzCbywSute0A_ejIYgQgmYwh3Vp9z7lxQbvCslPeNJ3jt98F3hczWdP2nq5B2vg4kcctyjd/s320/OutputDivAndCent.png&quot; height=&quot;151&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;

&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;</content><link rel='replies' type='application/atom+xml' href='http://cppmonastery.blogspot.com/feeds/5599643565393709148/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cppmonastery.blogspot.com/2012/08/console-tidy-output.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1543848439127765809/posts/default/5599643565393709148'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1543848439127765809/posts/default/5599643565393709148'/><link rel='alternate' type='text/html' href='http://cppmonastery.blogspot.com/2012/08/console-tidy-output.html' title='Dividing and Centering Text Output'/><author><name>TickNSwisted</name><uri>http://www.blogger.com/profile/10882833507254467617</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3WAnJDyhw3o1E_LsvJXX_ZEqL1nSHm_46Jg9SEh01QaNRCIVYw0Aibzn9EVN8umFL6osu3VzCbywSute0A_ejIYgQgmYwh3Vp9z7lxQbvCslPeNJ3jt98F3hczWdP2nq5B2vg4kcctyjd/s72-c/OutputDivAndCent.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1543848439127765809.post-8125872852707102268</id><published>2013-01-04T03:00:00.001-05:00</published><updated>2013-01-21T15:17:24.518-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Beginner"/><category scheme="http://www.blogger.com/atom/ns#" term="C++"/><category scheme="http://www.blogger.com/atom/ns#" term="Console Window"/><title type='text'>Console Window - Colored Text</title><content type='html'>&lt;style&gt;

#codeTable
{
    font-family: Consolas;
    font-style: normal;
    font-weight: normal;
    border:2px solid black;
    border-style: solid;
    border-color: #000000;
    background-color: white;
    text-align: left;
    color: black;

}

#codeOutput
{   
    padding-top:30px;
    padding-bottom:30px;
    padding-right:30px;
    padding-left:30px;
    font-family: &quot;Lucida Console&quot;, Courier New, Serif
    font-style: normal;
    font-weight: normal;
    font-size: 12 px;
    background-color: black;
    color: #cdcdcd;
    border:2px solid #cdcdcd;
    border-style: solid;
    border-color: #cdcdcd;
    background-color: #000000;
    text-align: left;
    color: #cdcdcd;
}

#codeNRef
{
    font-family: Consolas;
    font-style: normal;
    font-weight: normal;
    text-align: left;
    color: white;
}

#codeTitle
{
    background-color: dark gray;
    font-family: Arial;
    font-style: italic;
    font-weight: bold;
    font-size: 12px;
    text-align: center;
    color: black;
}

#codeFile
{
    font-family: &quot;Lucida Console&quot;, Courier New, Serif
    font-style: normal;
    font-weight: normal;
    font-size: 12 px;
    background-color: black;
    text-align:center;
    color: yellow;
}


#codeN
{
    font-family: Consolas;
    font-style: normal;
    font-weight: normal;
    text-align: left;
    color: black;
}

#codeG
{
    font-family: Consolas;
    font-style: normal;
    font-weight: normal;
    text-align: left;
    color: green;
}

#codeR
{
    font-family: Consolas;
    font-style: normal;
    font-weight: normal;
    text-align: left;
    color: #a31515;
}

#codeB
{
    font-family: Consolas;
    font-style: normal;
    font-weight: normal;
    text-align: left;
    color: blue;
}
&lt;/style&gt;

&lt;br /&gt;
&lt;h2&gt;
Colored Text&lt;/h2&gt;
If you&#39;ve ever wanted to use colors in a console window, as in having a&amp;nbsp;colorful text and&amp;nbsp;backgrounds, then are two primary methods:&lt;li&gt;the &lt;span id=&quot;codeNRef&quot;&gt;System ( )&lt;/span&gt; method&lt;/li&gt;&lt;li&gt;the &lt;span id=&quot;codeNRef&quot;&gt;SetConsoleTextAttribute ( )&lt;/span&gt; method&lt;/li&gt;&lt;br /&gt;&lt;br /&gt;The &lt;span id=&quot;codeNRef&quot;&gt;System ( )&lt;/span&gt; can be used on several operating systems but doesn&#39;t allow multiple colors to appear at once. &lt;span id=&quot;codeNRef&quot;&gt;SetConsoleTextAttribute ( )&lt;/span&gt; requires the &lt;span id=&quot;codeR&quot;&gt;&amp;lt;Windows.h&amp;gt;&lt;/span&gt; library is included but will allow multiple colors at once.&lt;br /&gt;&lt;br /&gt;Both use color codes of a hexadecimal notation, such as &lt;span id=&quot;codeNRef&quot;&gt;FF&lt;/span&gt;. The first digit is the background and the second is the text or foreground. For a complete color table, see the final output example.&lt;br /&gt;&lt;br /&gt;
&lt;h3&gt;
The System ( ) Method&lt;/h3&gt;
&lt;span id=&quot;codeNRef&quot;&gt;System(&lt;/span&gt;&lt;span id=&quot;codeR&quot;&gt;&quot;color F0&quot;&lt;/span&gt;&lt;span id=&quot;codeNRef&quot;&gt;);&lt;/span&gt; which changes the color of the environment. Consider the following:
&lt;br /&gt;
&lt;br /&gt;
&lt;table id=&quot;codeTable&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;
&lt;td id=&quot;codeTitle&quot;&gt;Using the system( ) function&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td id=&quot;codeFile&quot;&gt;main.cpp&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td id=&quot;CodeTable&quot;&gt;&lt;span id=&quot;codeG&quot;&gt;//===================================INCLUDES==================================&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;#include&lt;/span&gt; &lt;span id=&quot;codeR&quot;&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span id=&quot;codeG&quot;&gt;Assuming namespace ::std.&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;using namespace&lt;/span&gt; std;&lt;br /&gt;
&lt;br /&gt;
&lt;span id=&quot;codeG&quot;&gt;//==================================MAIN ENTRY=================================&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; main ( &lt;span id=&quot;codeB&quot;&gt;void&lt;/span&gt; )&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeG&quot;&gt;//Set the system color to 0xC1.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;system(&lt;span id=&quot;codeR&quot;&gt;&quot;color C1&quot;&lt;/span&gt;);&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeG&quot;&gt;//Output some text to the screen.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cout &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&quot;This text is made colorful by the system( ) function.&quot;&lt;/span&gt; &amp;lt;&amp;lt; endl;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeB&quot;&gt;return&lt;/span&gt; ( 0 );&lt;br /&gt;
}&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;br /&gt;

&lt;div style=&quot;text-align: center;&quot;&gt;Outputs:&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEivHPxfUDPUspTxd0vzIyLH6NPFYDE0jvydYepXsdWkDlgjhjYBUlAw5NB2eWLi85CH9R7vo5WVOw3ItekeMvTx2U0sx2oYycf6hed6eYC8jtobW-E8MZ7iyKEwtyCIcT8t9bFtFldyAQPm/s1600/systemcolorout.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEivHPxfUDPUspTxd0vzIyLH6NPFYDE0jvydYepXsdWkDlgjhjYBUlAw5NB2eWLi85CH9R7vo5WVOw3ItekeMvTx2U0sx2oYycf6hed6eYC8jtobW-E8MZ7iyKEwtyCIcT8t9bFtFldyAQPm/s400/systemcolorout.png&quot; height=&quot;188&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
SetConsoleTextAttribute ( ) Method
&lt;/h3&gt;
&lt;br /&gt;The &lt;span id=&quot;codeNRef&quot;&gt;SetConsoleTextAttribute ( )&lt;/span&gt; method allows for much more control over coloring text.





&lt;table id=&quot;codeTable&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;
&lt;td id=&quot;codeTitle&quot;&gt;Using the system( ) function&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td id=&quot;codeFile&quot;&gt;main.cpp&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td id=&quot;CodeTable&quot;&gt;&lt;span id=&quot;codeG&quot;&gt;//===================================INCLUDES==================================&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;#include&lt;/span&gt; &lt;span id=&quot;codeR&quot;&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;
&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;#include&lt;/span&gt; &lt;span id=&quot;codeR&quot;&gt;&amp;lt;Windows.h&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;
&lt;span id=&quot;codeG&quot;&gt;Assuming namespace ::std.&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;using namespace&lt;/span&gt; std;&lt;br /&gt;
&lt;br /&gt;
&lt;span id=&quot;codeG&quot;&gt;//==================================MAIN ENTRY=================================&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; main ( &lt;span id=&quot;codeB&quot;&gt;void&lt;/span&gt; )&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeG&quot;&gt;//Handle to console window&#39;s context.&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;HANDLE hConsole = GetStdHandle ( STD_OUTPUT_HANDLE );&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeG&quot;&gt;//Set the color (0x0E = Black Background, Yellow Foreground )&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;SetConsoleTextAttribute ( hConsole, 0x0E );&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cout &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&quot;Text made colorful by SetConsoleTextAttribute( ) function.&quot;&lt;/span&gt; &amp;lt;&amp;lt; endl;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id=&quot;codeB&quot;&gt;return&lt;/span&gt; ( 0 );&lt;br /&gt;
}&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;

&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;



&lt;h3&gt;
In-Depth SetConsoleTextAttribute ( ) Method&lt;/h3&gt;
In this example we&#39;ll create a function, &lt;span id=&quot;codeNRef&quot;&gt;GetColorCode ( )&lt;/span&gt; and loop through all the possible colors.

&lt;table id=&quot;codeTable&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;
&lt;td id=&quot;codeTitle&quot;&gt;The SetConsoleTextAttribute ( ) Method&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td id=&quot;codeFile&quot;&gt;main.cpp&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td id=&quot;CodeTable&quot;&gt;&lt;span id=&quot;codeG&quot;&gt;//===================================INCLUDES==================================&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;#include&lt;/span&gt; &lt;span id=&quot;codeR&quot;&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;#include&lt;/span&gt; &lt;span id=&quot;codeR&quot;&gt;&amp;lt;iomanip&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;#include&lt;/span&gt; &lt;span id=&quot;codeR&quot;&gt;&amp;lt;Windows.h&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;using namespace&lt;/span&gt; std;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span id=&quot;codeG&quot;&gt;//=============================FUNCTION PROTOTYPES=============================&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;unsigned char&lt;/span&gt; GetColorCode ( &lt;span id=&quot;codeB&quot;&gt;unsigned char&lt;/span&gt; colorBackground,&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;unsigned char&lt;/span&gt; colorForeground );&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span id=&quot;codeG&quot;&gt;//==================================MAIN ENTRY=================================&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; main( &lt;span id=&quot;codeB&quot;&gt;void&lt;/span&gt; )&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//handle to console window&#39;s context&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;unsigned char&lt;/span&gt; colorBackground = 0x00; &lt;span id=&quot;codeG&quot;&gt;//background color code&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;unsigned char&lt;/span&gt; colorForeground = 0x00;&lt;span id=&quot;codeG&quot;&gt;//foreground color code&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;unsigned char&lt;/span&gt; colorCode = 0x00; &lt;span id=&quot;codeG&quot;&gt;//high:background, low:foreground&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//---------------OUTPUT COLORFUL LITERAL---------------&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//set text color (0x08 = black background, grey foreground)&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SetConsoleTextAttribute ( hConsole, 0x08 );&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&quot;I don&#39;t always like &quot;&lt;/span&gt;;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//set text color (0x0E = black background, yellow foreground)&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SetConsoleTextAttribute(hConsole, 0x0E);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&quot;Cake&quot;&lt;/span&gt;;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//set text color (0x08 = black background, grey foreground)&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SetConsoleTextAttribute(hConsole, 0x08);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&quot;, but when I do it&#39;s always a &quot;&lt;/span&gt;;&lt;br /&gt;
&lt;br /&gt;
&lt;span id=&quot;codeG&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //set text color (0xC0 = red background, black foreground)&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SetConsoleTextAttribute(hConsole, 0xC0);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&quot;Lie!&quot;&lt;/span&gt; &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; endl;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//------------------OUTPUT COLOR CHART-----------------&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//output hexidecimal notation&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; setbase(16);&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//loop through each background color (0 to 15 = 16 possible colors)&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;for&lt;/span&gt; (colorBackground = 0; colorBackground &amp;lt; 16; colorBackground++)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&lt;span id=&quot;codeG&quot;&gt;//loop through each foreground color (0 to 15 = 16 possible colors)&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;for&lt;/span&gt; (colorForeground = 0; colorForeground &amp;lt; 16; colorForeground++)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//get color code as one variable&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; colorCode = GetColorCode(colorBackground, colorForeground);&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//set front and back colors&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SetConsoleTextAttribute(hConsole, colorCode);&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//output the color code&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; setfill(&lt;span id=&quot;codeR&quot;&gt;&#39;0&#39;&lt;/span&gt;)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//set fill character&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;&amp;lt; setw(2)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//create width for numbers&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;&amp;lt; uppercase&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//set output to uppercase&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&amp;lt; &lt;span id=&quot;codeB&quot;&gt;static_cast&lt;/span&gt;&amp;lt;&lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt;&amp;gt;(colorCode)&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//print colorCode as hex&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&#39; &#39;&lt;/span&gt;;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//padding for next value&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//start new line for next background color&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&#39;\n&#39;&lt;/span&gt;;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;return&lt;/span&gt; ( 0 );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span id=&quot;codeG&quot;&gt;//=============================FUNCTION DEFINITIONS============================&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;unsigned char&lt;/span&gt; GetColorCode ( &lt;span id=&quot;codeB&quot;&gt;unsigned char&lt;/span&gt; colorBackground,&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;unsigned char&lt;/span&gt; colorForeground )&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//return most signifigant bit of colorBackground and&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//least signifigant bit of colorForground as one byte&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;return&lt;/span&gt; (colorBackground &amp;lt;&amp;lt; 4) + colorForeground; &lt;br /&gt;
}&lt;br /&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;

&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;span style=&quot;color: #666666; font-family: Consolas; font-size: x-small;&quot;&gt;&lt;br /&gt;&amp;nbsp;&lt;span style=&quot;color: black;&quot;&gt;Outputs:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEinmm2pnIIbHDktGqQc1z5G6MtFEkHSLNkmYyrhYdmdHo3XTP5B1k9Xr-O-lRxDrMXy_n8UM6U8n_naXqLpZRR0vWGAoTntKbjlJCdcolqOJtP1gm4G1Bn5_nHP60hdh98tDUMz2QGfPC8e/s1600/ConsoleColor.bmp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEinmm2pnIIbHDktGqQc1z5G6MtFEkHSLNkmYyrhYdmdHo3XTP5B1k9Xr-O-lRxDrMXy_n8UM6U8n_naXqLpZRR0vWGAoTntKbjlJCdcolqOJtP1gm4G1Bn5_nHP60hdh98tDUMz2QGfPC8e/s320/ConsoleColor.bmp&quot; height=&quot;161&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
The program creates a &lt;span id=&quot;codeNRef&quot;&gt;HANDLE&lt;/span&gt; or a context of the console environment to affect the color code of that environment. Next we create an &lt;span id=&quot;codeB&quot;&gt;unsigned char&lt;/span&gt; for the &lt;span id=&quot;colorNRef&quot;&gt;colorBackground&lt;/span&gt;, the background of the text area, &lt;span id=&quot;colorNRef&quot;&gt;colorForeground&lt;/span&gt;, the foreground of the text itself, and &lt;span id=&quot;colorNRef&quot;&gt;colorCode&lt;/span&gt;, both the background and foreground combined into a single hexadecimal number.&lt;/br&gt;Since the &lt;span id=&quot;codeNRef&quot;&gt;colorCode&lt;/span&gt; is actually two hexidecimal digits, or places, which can never be negative we use &lt;span id=&quot;codeB&quot;&gt;unsigned&lt;/span&gt;. The reason we use &lt;span id=&quot;codeB&quot;&gt;char&lt;/span&gt;, despite the fact that the background color is only 4 bits, compared to the &lt;span id=&quot;codeB&quot;&gt;char&lt;/span&gt; being 8 bits, is because there is no smaller variable size availible (other than &lt;span id=&quot;codeB&quot;&gt;bool&lt;/span&gt;).&lt;br /&gt;&lt;br /&gt;We are only using four bits from each code, again, because the color code is only 8 bits, we must later take the four bits from &lt;span id=&quot;codeNRef&quot;&gt;ColorBackground&lt;/span&gt; (which are the left-most, or the&amp;nbsp;high bits) and add in the &lt;span id=&quot;codeNRef&quot;&gt;colorForground&lt;/span&gt; bits.&lt;br /&gt;&lt;br /&gt;To make this intuitive:&lt;br /&gt;&lt;span id=&quot;codeNRef&quot;&gt;colorBackground = 1111 0000b;&lt;/span&gt;&lt;br /&gt;&lt;span id=&quot;codeNRef&quot;&gt;colorForeground = 0000 1111b;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;After we call&amp;nbsp;&lt;span id=&quot;codeNRef&quot;&gt;GetColorCode(colorBackground, colorForeground)&lt;/span&gt;
the &lt;span id=&quot;codeNRef&quot;&gt;colorCode&lt;/span&gt; would be equal to &lt;span id=&quot;codeNRef&quot;&gt;1111 1111b&lt;/span&gt;.</content><link rel='replies' type='application/atom+xml' href='http://cppmonastery.blogspot.com/feeds/8125872852707102268/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cppmonastery.blogspot.com/2012/08/different-colors-in-console-window.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1543848439127765809/posts/default/8125872852707102268'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1543848439127765809/posts/default/8125872852707102268'/><link rel='alternate' type='text/html' href='http://cppmonastery.blogspot.com/2012/08/different-colors-in-console-window.html' title='Console Window - Colored Text'/><author><name>TickNSwisted</name><uri>http://www.blogger.com/profile/10882833507254467617</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEivHPxfUDPUspTxd0vzIyLH6NPFYDE0jvydYepXsdWkDlgjhjYBUlAw5NB2eWLi85CH9R7vo5WVOw3ItekeMvTx2U0sx2oYycf6hed6eYC8jtobW-E8MZ7iyKEwtyCIcT8t9bFtFldyAQPm/s72-c/systemcolorout.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1543848439127765809.post-1318036791192852124</id><published>2012-12-28T03:00:00.000-05:00</published><updated>2013-01-21T16:07:13.969-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Beginner"/><category scheme="http://www.blogger.com/atom/ns#" term="C++"/><category scheme="http://www.blogger.com/atom/ns#" term="Console Window"/><title type='text'>Console Window - Christmas Tree</title><content type='html'>&lt;style&gt;

#codeTable
{
    font-family: Consolas;
    font-style: normal;
    font-weight: normal;
    border:2px solid black;
    border-style: solid;
    border-color: #000000;
    background-color: white;
    text-align: left;
    color: black;

}

#codeOutput
{   
    padding-top:30px;
    padding-bottom:30px;
    padding-right:30px;
    padding-left:30px;
    font-family: &quot;Lucida Console&quot;, Courier New, Serif
    font-style: normal;
    font-weight: normal;
    font-size: 12 px;
    background-color: black;
    color: #cdcdcd;
    border:2px solid #cdcdcd;
    border-style: solid;
    border-color: #cdcdcd;
    background-color: #000000;
    text-align: left;
    color: #cdcdcd;
}

#codeNRef
{
    font-family: Consolas;
    font-style: normal;
    font-weight: normal;
    text-align: left;
    color: white;
}

#codeTitle
{
    background-color: dark gray;
    font-family: Arial;
    font-style: italic;
    font-weight: bold;
    font-size: 12px;
    text-align: center;
    color: black;
}

#codeFile
{
    font-family: &quot;Lucida Console&quot;, Courier New, Serif
    font-style: normal;
    font-weight: normal;
    font-size: 12 px;
    background-color: black;
    text-align:center;
    color: yellow;
}


#codeN
{
    font-family: Consolas;
    font-style: normal;
    font-weight: normal;
    text-align: left;
    color: black;
}

#codeG
{
    font-family: Consolas;
    font-style: normal;
    font-weight: normal;
    text-align: left;
    color: green;
}

#codeR
{
    font-family: Consolas;
    font-style: normal;
    font-weight: normal;
    text-align: left;
    color: #a31515;
}

#codeB
{
    font-family: Consolas;
    font-style: normal;
    font-weight: normal;
    text-align: left;
    color: blue;
}
&lt;/style&gt;
&lt;br /&gt;
&lt;h2&gt;
Console Window Christmas Tree&lt;/h2&gt;
As a festive programming challenge, programmers are tasked with creating a Christmas Tree out of asterisks, &#39;&lt;span id=&quot;codeNRef&quot;&gt;*&lt;/span&gt;&#39;, which is the ASCII character code &lt;span id=&quot;codeR&quot;&gt;&#39;\xCE&#39;&lt;/span&gt;. The purpose of this challenge is to use &lt;span id=&quot;codeB&quot;&gt;for&lt;/span&gt; loops and &lt;span id=&quot;codeNRef&quot;&gt;setw( )&lt;/span&gt; from the &lt;span id=&quot;codeR&quot;&gt;&amp;lt;iomanip&amp;gt;&lt;/span&gt; library to create &lt;em&gt;The Desired Output&lt;/em&gt;. As such, you cannot simply output a &lt;span id=&quot;codeNRef&quot;&gt;string&lt;/span&gt; of spaces and asterisks.&lt;br /&gt;
&lt;br /&gt;
The solution requires an understanding of for loops and an understanding of what is necessary mathematically. Don&#39;t let that scare you, it&#39;s not difficult math: Addition, subtraction and possibly multiplication. There are many possible ways to achieve the goal. This is a problem which requires some complex thought so don&#39;t get discouraged if it doesn&#39;t come to you right away.&lt;br /&gt;
&lt;br /&gt;
To start, let&#39;s take a look at &lt;em&gt;The Desired Output&lt;/em&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;table id=&quot;codeTable&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;
&lt;td id=&quot;codeTitle&quot;&gt;The Desired Output&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td id=&quot;codeOutput&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;*&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;***&lt;br /&gt;
&amp;nbsp;&amp;nbsp;*****&lt;br /&gt;
&amp;nbsp;*******&lt;br /&gt;
*********&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
Tackling the Tree&lt;/h3&gt;
First, we notice that the count of characters, number of asterisks,&amp;nbsp;that are output each line starts with one character and increases by&amp;nbsp;two every line after. Also, starting with the number one and adding two each time ensures that the number will always be the same, either an even or odd value, that it was originally.&lt;br /&gt;
&lt;br /&gt;
Why is it important that the number of characters being output is an odd count? It is necessary so that a single character acting as the center of the tree. If we had an even number of characters there wouldn&#39;t be one center character and the tree would not be as centered.&lt;br /&gt;
&lt;br /&gt;
Knowing that we will want to store the number of characters we are currently sending to the output we can declare an &lt;span id=&quot;codeB&quot;&gt;unsigned int&lt;/span&gt; variable, which we&#39;ll name &lt;span id=&quot;codeNRef&quot;&gt;x&lt;/span&gt;. We&#39;ll also want to declare a variable to hold the current line, which will hold our current line, &lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;unsigned int&lt;/span&gt; &lt;span id=&quot;codeNRef&quot;&gt;y&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
The number of lines, the final and desired height of the tree, which will not change throughout the program we can declare as an &lt;span id=&quot;codeB&quot;&gt;const unsigned int&lt;/span&gt; variable, &lt;span id=&quot;codeNRef&quot;&gt;HEIGHT&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
How do we know where to go from there? Well, that&#39;s tricky but we know that we&#39;re going to need a basic program setup so let&#39;s start with what we have so far:&lt;br /&gt;
&lt;br /&gt;
&lt;table id=&quot;codeTable&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;
&lt;td id=&quot;codeTitle&quot;&gt;The Start of the Tree&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td id=&quot;codeFile&quot;&gt;main.cpp&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td id=&quot;CodeTable&quot;&gt;&lt;span id=&quot;codeG&quot;&gt;//===================================INCLUDES==================================&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;#include&lt;/span&gt; &lt;span id=&quot;codeR&quot;&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//Included for text output, cout.&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;#include&lt;/span&gt; &lt;span id=&quot;codeR&quot;&gt;&amp;lt;iomanip&amp;gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//Included for setw(), fill(), output manipulation.&lt;br /&gt;&lt;br /&gt;//Assuming namespace ::std.&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;using namespace&lt;/span&gt; std;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span id=&quot;codeG&quot;&gt;//==================================MAIN ENTRY=================================&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; main( &lt;span id=&quot;codeB&quot;&gt;void&lt;/span&gt; )&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//The desired height of the tree.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;const unsigned int&lt;/span&gt; HEIGHT = 5;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//The current width of the tree.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;unsigned int&lt;/span&gt; x;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//The current height of the tree.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;unsigned int&lt;/span&gt; y;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//The for loop solution&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //...&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //End the program, returning zero.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;return&lt;/span&gt; ( 0 );&lt;br /&gt;
}&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;br /&gt;
Using what we know, that the&amp;nbsp;&lt;span style=&quot;font-family: Consolas;&quot;&gt;x&lt;/span&gt; variable depends on the &lt;span style=&quot;font-family: Consolas;&quot;&gt;y&lt;/span&gt;, we can assume the loop dealing with height will be the first loop which will then have a second loop that outputs the correct number of characters.&lt;br /&gt;
&lt;br /&gt;
The count of characters from left to right, will be depends on the height. We know that the height, loop dealing with the &lt;span style=&quot;font-family: Consolas;&quot;&gt;y&lt;/span&gt; will be the outer-most &lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;for&lt;/span&gt; loop and that the inner-loop, the x loop, will have a condition which is directly affected by the current line number.&lt;br /&gt;
&lt;br /&gt;
&lt;table id=&quot;codeTable&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;
&lt;td id=&quot;codeTitle&quot;&gt;Part of a Possible Solution&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td id=&quot;codeFile&quot;&gt;main.cpp, for loop part 1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td id=&quot;CodeTable&quot;&gt;&lt;span id=&quot;codeG&quot;&gt;//Set current line, y, to 1 since we&#39;ll need it&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeG&quot;&gt;// to calculate number of asterisks to output, x.&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;for&lt;/span&gt; ( y = 1; y &amp;lt; HEIGHT; y++ )&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//Set width before the asterisk(s) to HEIGHT minus the current line, y.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; setw( HEIGHT - y );&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//Consider what conditional would be appropriate.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;for&lt;/span&gt; ( x = 1; x &amp;lt; &lt;strong&gt;conditional&lt;/strong&gt; ; x++ )&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&#39;*&#39;&lt;/span&gt;;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; endl;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
Try a few things, again -- there are many possible solutions. The following is the complete code of one solution.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
One Possible Complete Solution:&lt;/h3&gt;
&lt;br /&gt;
&lt;table id=&quot;codeTable&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;
&lt;td id=&quot;codeTitle&quot;&gt;A Completed Christmas Tree&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td id=&quot;codeFile&quot;&gt;main.cpp&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td id=&quot;codeTable&quot;&gt;&lt;br /&gt;
&lt;span id=&quot;codeG&quot;&gt;//===================================INCLUDES==================================&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;#include&lt;/span&gt; &lt;span id=&quot;codeR&quot;&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//Included for text output, cout.&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;#include&lt;/span&gt; &lt;span id=&quot;codeR&quot;&gt;&amp;lt;iomanip&amp;gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//Included for setw(), fill(), output manipulation.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span id=&quot;codeG&quot;&gt;//Assuming namespace ::std.&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;using namespace&lt;/span&gt; std;&lt;br /&gt;
&lt;br /&gt;
&lt;span id=&quot;codeG&quot;&gt;//==================================MAIN ENTRY=================================&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;codeB&quot;&gt;int&lt;/span&gt; main( &lt;span id=&quot;codeB&quot;&gt;void&lt;/span&gt; )&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//The desired height of the tree, plus one.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;const unsigned int&lt;/span&gt; HEIGHT = 6;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//The current width of the tree.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;unsigned int&lt;/span&gt; x;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//The current height of the tree.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;unsigned int&lt;/span&gt; y;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//The for loop solution.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//Loop through each line, up to a count of HEIGHT - 1 times.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;for&lt;/span&gt; ( y = 1; y &amp;lt; HEIGHT; y++ )&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//Set width before the asterisk(s) to HEIGHT minus the current line, y.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; setw( HEIGHT - y );&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//Loop until the current asterisk, x, is less than twice the line, y.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;for&lt;/span&gt; ( x = 1; x &amp;lt; y * 2; x++ )&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//Output the asterisk.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; &lt;span id=&quot;codeR&quot;&gt;&#39;*&#39;&lt;/span&gt;;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//End the output to the current line.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; endl;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeG&quot;&gt;//End the program, returning zero.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span id=&quot;codeB&quot;&gt;return&lt;/span&gt; ( 0 );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
</content><link rel='replies' type='application/atom+xml' href='http://cppmonastery.blogspot.com/feeds/1318036791192852124/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cppmonastery.blogspot.com/2012/08/christmas-tree.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1543848439127765809/posts/default/1318036791192852124'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1543848439127765809/posts/default/1318036791192852124'/><link rel='alternate' type='text/html' href='http://cppmonastery.blogspot.com/2012/08/christmas-tree.html' title='Console Window - Christmas Tree'/><author><name>TickNSwisted</name><uri>http://www.blogger.com/profile/10882833507254467617</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>