<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Talk Binary &#187; C++</title>
	<atom:link href="http://talkbinary.com/category/programming/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://talkbinary.com</link>
	<description>Programming Resources, Technology, Computers</description>
	<lastBuildDate>Wed, 15 Feb 2012 16:01:31 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>How to Send Email Through Gmail Using Libcurl</title>
		<link>http://talkbinary.com/programming/c/how-to-send-email-through-gmail-using-libcurl/</link>
		<comments>http://talkbinary.com/programming/c/how-to-send-email-through-gmail-using-libcurl/#comments</comments>
		<pubDate>Wed, 15 Feb 2012 04:53:36 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=4274</guid>
		<description><![CDATA[I needed to send an e-mail through my C++ program and was able to do it using Libcurl. Since I had a hard time initially, I decided to share the following with you. Download libcurl First download libcurl. I found binaries for my OS, Mac OSX, with some instructions on how to unpackage it making<a class="moretag" href="http://talkbinary.com/programming/c/how-to-send-email-through-gmail-using-libcurl/">&#160;&#160;Full Article&#8230;</a>
]]></description>
				<content:encoded><![CDATA[<p>I needed to send an e-mail through my C++ program and was able to do it using Libcurl. Since I had a hard time initially, I decided to share the following with you. </p>
<h2 id="section-1">Download libcurl</h2>
<hr />
First download <a href="http://curl.haxx.se/">libcurl</a>. I found binaries for my OS, Mac OSX, with some instructions on how to unpackage it making installation a breeze. </p>
<h2 id="section-2">Compiling a Sample SMTP Program with Authentication and Transport Security</h2>
<hr />
I found a sample <a href="http://curl.haxx.se/libcurl/c/smtp-tls.html">smtp-tls</a> program in which I simply saved and compiled to make sure libcurl was linking properly. If it isn&#8217;t, try compiling it in the following manner:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">g++</span> smtp-tls.c <span style="color: #660033;">-o</span> smtp-tls <span style="color: #660033;">-lcurl</span></pre></td></tr></table></div>

<h2 id="section-3">Modifying the Sample to Work with GMail</h2>
<hr />
I found the configuration instructions in the following support page: <a href="http://support.google.com/mail/bin/answer.py?hl=en&#038;answer=13287">Configuring Other Mail Clients</a>.</p>
<p>I modified the following lines in the smtp-tls program to make it work with GMail.</p>
<p>First, I changed the following FROM, TO, CC variables from</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#define FROM    &quot;&lt;sender@example.org&gt;&quot;</span>
<span style="color: #339900;">#define TO      &quot;&lt;addressee@example.net&gt;&quot;</span>
<span style="color: #339900;">#define CC      &quot;&lt;info@example.org&gt;&quot;</span></pre></td></tr></table></div>

<p>to the following.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#define FROM    &quot;&lt;myemailaddress@gmail.com&gt;&quot;</span>
<span style="color: #339900;">#define TO      &quot;&lt;myemailaddress@gmail.com&gt;&quot;</span>
<span style="color: #339900;">#define CC      &quot;&lt;myemailaddress@gmail.com&gt;&quot;</span></pre></td></tr></table></div>

<p>Then I changed the following line setting the URL from</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;">curl_easy_setopt<span style="color: #008000;">&#40;</span>curl, CURLOPT_URL, <span style="color: #FF0000;">&quot;smtp://mainserver.example.net:587&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>to the following.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;">curl_easy_setopt<span style="color: #008000;">&#40;</span>curl, CURLOPT_URL, <span style="color: #FF0000;">&quot;smtp://smtp.gmail.com:587&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>I then commented out the following line</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">//curl_easy_setopt(curl, CURLOPT_CAINFO,&quot;/path/to/certificate.pem&quot;);</span></pre></td></tr></table></div>

<p>since I kept receiving errors about certificates and wasn&#8217;t sure how to fix this. (If this isn&#8217;t done properly, please let me know otherwise!) </p>
<p>Afterwards, I simply put my e-mail address and password</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;">    curl_easy_setopt<span style="color: #008000;">&#40;</span>curl, CURLOPT_USERNAME, <span style="color: #FF0000;">&quot;myemailaddress@gmail.com&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    curl_easy_setopt<span style="color: #008000;">&#40;</span>curl, CURLOPT_PASSWORD, <span style="color: #FF0000;">&quot;PASSWORD123&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>The console looked in the following manner:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">*</span> About to connect<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> to smtp.gmail.com port <span style="color: #000000;">587</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #666666; font-style: italic;">#0)</span>
<span style="color: #000000; font-weight: bold;">*</span>   Trying 74.125.53.109... <span style="color: #000000; font-weight: bold;">*</span> connected
<span style="color: #000000; font-weight: bold;">*</span> Connected to smtp.gmail.com <span style="color: #7a0874; font-weight: bold;">&#40;</span>74.125.53.109<span style="color: #7a0874; font-weight: bold;">&#41;</span> port <span style="color: #000000;">587</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #666666; font-style: italic;">#0)</span>
<span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #000000;">220</span> mx.google.com ESMTP q10sm8033678pbb.10
<span style="color: #000000; font-weight: bold;">&gt;</span> EHLO unknownc42c0304eee8
<span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #000000;">250</span>-mx.google.com at your service, <span style="color: #7a0874; font-weight: bold;">&#91;</span>99.53.224.80<span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #000000;">250</span>-SIZE <span style="color: #000000;">35882577</span>
<span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #000000;">250</span>-8BITMIME
<span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #000000;">250</span>-STARTTLS
<span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #000000;">250</span> ENHANCEDSTATUSCODES
<span style="color: #000000; font-weight: bold;">&gt;</span> STARTTLS
<span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #000000;">220</span> 2.0.0 Ready to start TLS
<span style="color: #000000; font-weight: bold;">*</span> SSL connection using RC4-SHA
<span style="color: #000000; font-weight: bold;">*</span> Server certificate:
<span style="color: #000000; font-weight: bold;">*</span> 	 subject: <span style="color: #007800;">C</span>=US; <span style="color: #007800;">ST</span>=California; <span style="color: #007800;">L</span>=Mountain View; <span style="color: #007800;">O</span>=Google Inc; <span style="color: #007800;">CN</span>=smtp.gmail.com
<span style="color: #000000; font-weight: bold;">*</span> 	 start date: <span style="color: #000000;">2011</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">18</span> 01:<span style="color: #000000;">57</span>:<span style="color: #000000;">17</span> GMT
<span style="color: #000000; font-weight: bold;">*</span> 	 expire date: <span style="color: #000000;">2012</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">18</span> 02:07:<span style="color: #000000;">17</span> GMT
<span style="color: #000000; font-weight: bold;">*</span> 	 common name: smtp.gmail.com <span style="color: #7a0874; font-weight: bold;">&#40;</span>matched<span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">*</span> 	 issuer: <span style="color: #007800;">C</span>=US; <span style="color: #007800;">O</span>=Google Inc; <span style="color: #007800;">CN</span>=Google Internet Authority
<span style="color: #000000; font-weight: bold;">*</span> 	 SSL certificate verify ok.
<span style="color: #000000; font-weight: bold;">&gt;</span> EHLO unknownc42c0304eee8
<span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #000000;">250</span>-mx.google.com at your service, <span style="color: #7a0874; font-weight: bold;">&#91;</span>99.53.224.80<span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #000000;">250</span>-SIZE <span style="color: #000000;">35882577</span>
<span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #000000;">250</span>-8BITMIME
<span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #000000;">250</span>-AUTH LOGIN PLAIN XOAUTH
<span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #000000;">250</span> ENHANCEDSTATUSCODES
<span style="color: #000000; font-weight: bold;">&gt;</span> AUTH PLAIN <span style="color: #007800;">a3VyaXlha2lAA21haWwuY291AGt1cml5YWtpQGdtYWlsLmNvbQBHTUF0b3MyNzEzdB</span>==
<span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #000000;">235</span> 2.7.0 Accepted
<span style="color: #000000; font-weight: bold;">&gt;</span> MAIL FROM:<span style="color: #000000; font-weight: bold;">&lt;</span>myemailaddress<span style="color: #000000; font-weight: bold;">@</span>gmail.com<span style="color: #000000; font-weight: bold;">&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #000000;">250</span> 2.1.0 OK q10sm8033678pbb.10
<span style="color: #000000; font-weight: bold;">&gt;</span> RCPT TO:<span style="color: #000000; font-weight: bold;">&lt;</span>myemailaddress<span style="color: #000000; font-weight: bold;">@</span>gmail.com<span style="color: #000000; font-weight: bold;">&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #000000;">250</span> 2.1.5 OK q10sm8033678pbb.10
<span style="color: #000000; font-weight: bold;">&gt;</span> RCPT TO:<span style="color: #000000; font-weight: bold;">&lt;</span>myemailaddress<span style="color: #000000; font-weight: bold;">@</span>gmail.com<span style="color: #000000; font-weight: bold;">&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #000000;">250</span> 2.1.5 OK, duplicate recipients will be consolidated. q10sm8033678pbb.10
<span style="color: #000000; font-weight: bold;">&gt;</span> DATA
<span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #000000;">354</span>  Go ahead q10sm8033678pbb.10
<span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #000000;">250</span> 2.0.0 OK <span style="color: #000000;">1329280154</span> q10sm8033678pbb.10
<span style="color: #000000; font-weight: bold;">*</span> Connection <span style="color: #666666; font-style: italic;">#0 to host smtp.gmail.com left intact</span>
<span style="color: #000000; font-weight: bold;">&gt;</span> QUIT
<span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #000000;">221</span> 2.0.0 closing connection q10sm8033678pbb.10
<span style="color: #000000; font-weight: bold;">*</span> Closing connection <span style="color: #666666; font-style: italic;">#0</span></pre></td></tr></table></div>

<p>I checked my e-mail, and there it was! If you wish to modify the contents of the e-mail, simply modify the payload. </p>
<p>Let me know if you encountered any problems. </p>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/programming/c/how-to-send-email-through-gmail-using-libcurl/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>C++ &#8211; Scope of Variables</title>
		<link>http://talkbinary.com/programming/c/c-scope-of-variables/</link>
		<comments>http://talkbinary.com/programming/c/c-scope-of-variables/#comments</comments>
		<pubDate>Tue, 15 Feb 2011 17:23:24 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[c++ global variables]]></category>
		<category><![CDATA[c++ local variables]]></category>
		<category><![CDATA[c++ scope of variables]]></category>
		<category><![CDATA[scope of variables]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=3601</guid>
		<description><![CDATA[The scope of a variable determines the block of instructions in which a variable may be accessed. To understand this topic, we&#8217;ll start by discussing local variables. Local Variables Local variables may be accessed within the block of instructions in which they were declared. For example, when we write a main function, all variables declared<a class="moretag" href="http://talkbinary.com/programming/c/c-scope-of-variables/">&#160;&#160;Full Article&#8230;</a>
]]></description>
				<content:encoded><![CDATA[<p>The scope of a variable determines the block of instructions in which a variable may be accessed. To understand this topic, we&#8217;ll start by discussing local variables. </p>
<h3 id="section-1">Local Variables</h3>
<p>Local variables may be accessed within the block of instructions in which they were declared. For example, when we write a main function, all variables declared in that block of code may be accessed any place after they have been initialized.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> 
    <span style="color: #0000ff;">int</span> a, b<span style="color: #008080;">;</span>
    string message <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;The sum of a and b is: &quot;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> a <span style="color: #000080;">&gt;&gt;</span> b<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> message <span style="color: #000080;">&lt;&lt;</span> a <span style="color: #000040;">+</span> b <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p><span id="more-3601"></span><br />
Variables declared within a block of instructions such as an if-else statement or loop may only be used local to that block as well.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> n <span style="color: #000080;">=</span> <span style="color: #0000dd;">10</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">int</span> res <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;=</span> n<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
     <span style="color: #0000ff;">int</span> a <span style="color: #000080;">=</span> i<span style="color: #000040;">*</span>i<span style="color: #008080;">;</span>
     res <span style="color: #000040;">+</span><span style="color: #000080;">=</span> a<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> res <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>In the example above, variable <strong>a</strong> may only be accessed within the for loop. </p>
<p>As you may now know, variables declared within functions are also only local to that function.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> factorial<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
&nbsp;
     <span style="color: #0000ff;">int</span> factorial <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
&nbsp;
     <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;=</span> n<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> 
          factorial <span style="color: #000040;">*</span><span style="color: #000080;">=</span> i<span style="color: #008080;">;</span>
     <span style="color: #008000;">&#125;</span>
&nbsp;
     <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> 
    <span style="color: #0000ff;">int</span> n<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> n<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">int</span> fac <span style="color: #000080;">=</span> factorial<span style="color: #008000;">&#40;</span>n<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Factorial of n: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> fac <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<h3 id="section-2">Global Variables</h3>
<p>Global variables on the other hand may be accessed within every scope of the program. The use of global variables is usually discouraged since they may be modified from anywhere. They may be used to avoid having to pass in a variable that remains continuous throughout the program.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> bonus <span style="color: #000080;">=</span> <span style="color: #0000dd;">5</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">bool</span> flag <span style="color: #000080;">=</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// This function increases salary and adds the bonus</span>
<span style="color: #666666;">// if the flag is true</span>
<span style="color: #0000ff;">int</span> increaseSalary<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> 
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> flag <span style="color: #008000;">&#41;</span> 
        <span style="color: #0000ff;">return</span> n <span style="color: #000040;">+</span> bonus<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">else</span> 
        <span style="color: #0000ff;">return</span> n<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">// This function increases the bonus</span>
<span style="color: #0000ff;">void</span> increaseBonus<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> 
    bonus <span style="color: #000040;">+</span><span style="color: #000080;">=</span> n<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">// This function changes the flag </span>
<span style="color: #0000ff;">void</span> setFlag<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">bool</span> n<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> 
    flag <span style="color: #000080;">=</span> n<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;The bonus is currently: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> bonus <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">int</span> mySalary <span style="color: #000080;">=</span> <span style="color: #0000dd;">10</span><span style="color: #008080;">;</span>
    mySalary <span style="color: #000040;">+</span><span style="color: #000080;">=</span> increaseSalary<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">10</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    setFlag<span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>flag<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    mySalary <span style="color: #000040;">+</span><span style="color: #000080;">=</span> increaseSalary<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">10</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;My Salary is now: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> mySalary <span style="color: #000080;">&lt;&lt;</span> endl
    <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<blockquote><h4 id="section-3">Only use global variables when absolutely needed</h4>
<p>Remember, only use global variables if you absolutely need to. It is very cumbersome to debug a program with global variables if several functions are accessing and modifying their values. A good way to use them is possibly for a flag to run auxiliary functions to help decode a program if it its true.</p></blockquote>
<h3 id="section-4">Going further</h3>
<p>Now that you understand how the scope of variables work, try answering the following questions.</p>
<ol>
<li>Can two different functions declare variables with the same names?</li>
<li>If you declare a variable within the <strong>if</strong> clause, may you access it in the <strong>else</strong> clause?</li>
<li>If you declare a variable <strong>count</strong> in an if statement, can you access it within a nested if statement within that original if statement?</li>
<li>What is the output up the previous global variable example? What is the value of <strong>mySalary?</strong></li>
<li>Write a program with a global boolean flag that determines whether to display auxiliary <strong>cout</strong> statements to help debug your code. (Example: Print out factorial at every iteration.)</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/programming/c/c-scope-of-variables/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>C++ Functions</title>
		<link>http://talkbinary.com/programming/c/c-functions/</link>
		<comments>http://talkbinary.com/programming/c/c-functions/#comments</comments>
		<pubDate>Sat, 11 Sep 2010 14:00:30 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[basic error checking]]></category>
		<category><![CDATA[c++ function]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[summation function]]></category>
		<category><![CDATA[void function]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=3372</guid>
		<description><![CDATA[A function gives the user the ability to execute a block of instructions on a set of inputs to receive a particular output. Functions are useful when the user needs to execute a set of instructions many times with different parameters. The syntax for a function is below: return_type name&#40;parameter1, parameter2, ...&#41; &#123; statements &#125;<a class="moretag" href="http://talkbinary.com/programming/c/c-functions/">&#160;&#160;Full Article&#8230;</a>
]]></description>
				<content:encoded><![CDATA[<p>A function gives the user the ability to execute a block of instructions on a set of inputs to receive a particular output. Functions are useful when the user needs to execute a set of instructions many times with different parameters. The syntax for a function is below:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;">return_type name<span style="color: #008000;">&#40;</span>parameter1, parameter2, ...<span style="color: #008000;">&#41;</span> 
<span style="color: #008000;">&#123;</span>
     statements
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>The function is easily broken down into the following parts:</p>
<ol>
<li>The <em>return_type</em> denotes the type of object that the function will return.</li>
<li>The <em>name</em> denotes the identifier for the function.</li>
<li>The <em>parameters</em> are the inputs for the function. The number of parameters may range from zero or to as many as you need and are separated by commas. The parameters are specified with the type of object and identifier.</li>
<li>The <em>statements</em> are the set of instructions executed when the function is called.</li>
</ol>
<p><span id="more-3372"></span></p>
<h3 id="section-1">Function Example</h3>
<p>For example, we could create a function that would return the summation of n. Before functions, we&#8217;d have to write the same set of instructions multiple times if we wanted to find the result more than one time. The code is below.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;">&nbsp;
<span style="color: #339900;">#include &lt;iostream&gt;</span>
&nbsp;
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> summation<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
&nbsp;
    <span style="color: #0000ff;">int</span> total <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>   
&nbsp;
    <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;=</span> n<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span> <span style="color: #008000;">&#41;</span> 
    <span style="color: #008000;">&#123;</span>
        total <span style="color: #000040;">+</span><span style="color: #000080;">=</span> i<span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0000ff;">return</span> total<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
&nbsp;
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> summation<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">5</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Now, let&#8217;s go ahead and break down the previous code. </p>
<p>The following line of code calls the function with a parameter of <em>5</em>. We could have otherwise done it with an integer variable.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;">summation<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">5</span><span style="color: #008000;">&#41;</span></pre></td></tr></table></div>

<p>The following is the function header. This function is returning an <em>int</em> value, it&#8217;s identifier is <em>summation</em> and it only has one <em>integer</em> parameter <em>n</em>.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> summation<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span></pre></td></tr></table></div>

<p>Now, we&#8217;ll skip the actual calculation of the summation because by this time, you should be able to do these type of problems.</p>
<p>What is interesting is the following statement. It marks the end of the function as it returns an integer variable, <em>total</em>.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">return</span> total<span style="color: #008080;">;</span></pre></td></tr></table></div>

<h3 id="section-2">Basic error handling with multiple return statements</h3>
<p>We could easily add multiple return statements for different cases of input. One popular case is when the user provided an erroneous input. For example a negative value for our summation function would result in undesired behavior. To prevent this, we could easily add the following before line 8.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> n <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">0</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Why <em>-1</em>? Well, we know by the definition of the sum, <em>n</em> must be 1 or greater. Therefore, if we receive a <em>-1</em> as an answer, we know the user did something wrong. We could&#8217;ve easily added a <strong>cout</strong> statement letting the user know we didn&#8217;t like the previous input. Go ahead, try it.</p>
<p>Error checking is crucial for having programs that run correctly. We don&#8217;t want our program to use an invalid sum value if we need it later on do. To fix this, we could either terminate the program or ask the user to provide a valid value again.</p>
<h3 id="section-3">Void functions</h3>
<p>There are going to be times when we will need a function to simply execute some instructions without returning a value. </p>
<p>For example, let&#8217;s create a function that asks the user for his or her name and greets them.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> greeting<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
&nbsp;
    string name<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> name<span style="color: #008080;">;</span>
&nbsp;
     <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Hello, &quot;</span> <span style="color: #000080;">&lt;&lt;</span> name <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;!&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
     <span style="color: #0000ff;">return</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>The following function would be called with the following call:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;">greeting<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>If you noticed, this function does not return any type of object. This is a <em>void</em> function.</p>
<h3 id="section-4">Going further with functions</h3>
<p>In the next tutorial, we&#8217;ll talk about the scope of variables. This will teach you about global and local variables within your program.</p>
<p>For now, why don&#8217;t you try the following problems?</p>
<ul>
<li>Create a factorial function.</li>
<li>Create a function that adds two integer values together and returns the result</li>
<p><il>Finish the program described in the tutorial if you haven&#8217;t already. </p>
<ol>
<li>Make sure you created the summation function with the error checking described before</li>
<li>In your main program, ask the user for an input and determine if it is valid. If it isn&#8217;t, ask the user to input another value again.</li>
<li>Now, instead of asking the user for input again, simply terminate the program with an error message</li>
</ol>
</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/programming/c/c-functions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C++ Do While Loop</title>
		<link>http://talkbinary.com/programming/c/c-do-while-loop/</link>
		<comments>http://talkbinary.com/programming/c/c-do-while-loop/#comments</comments>
		<pubDate>Thu, 19 Aug 2010 16:00:38 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[C++ Do While Loop]]></category>
		<category><![CDATA[control flow]]></category>
		<category><![CDATA[Control Structure]]></category>
		<category><![CDATA[Do While Loop]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=3223</guid>
		<description><![CDATA[The Do While Loop is different from the While Loop for one reason only. The condition is checked after executing the statements. Therefore, this loop guarantees that the statements will be executed at least once. The syntax for the Do While Loop is below: do &#123; &#160; // Statements ... &#160; &#125; while &#40; condition<a class="moretag" href="http://talkbinary.com/programming/c/c-do-while-loop/">&#160;&#160;Full Article&#8230;</a>
]]></description>
				<content:encoded><![CDATA[<p>The Do While Loop is different from the <a href="http://talkbinary.com/programming/c/c-while-loop/">While Loop</a> for one reason only. The condition is checked after executing the statements. Therefore, this loop guarantees that the statements will be executed at least once. </p>
<p><center><div id="attachment_3224" class="wp-caption alignnone" style="width: 412px"><img src="http://talkbinary.com/wp-content/uploads/2010/08/DoWhile.jpg" alt="Do While Loop" title="DoWhile" width="402" height="377" class="size-full wp-image-3224" /><p class="wp-caption-text">Do While Loop</p></div></center></p>
<p>The syntax for the Do While Loop is below:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">do</span> <span style="color: #008000;">&#123;</span> 
&nbsp;
<span style="color: #666666;">// Statements ...</span>
&nbsp;
<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span> condition <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<h3 id="section-1">Do While Example</h3>
<p>An example of a do while loop is below.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;">string msg<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">do</span> <span style="color: #008000;">&#123;</span> 
   <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Enter a word or 'exit' to quit: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
   <span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> msg<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span> msg <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;exit&quot;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>Do While Loops aren&#8217;t used as extensively as <a href="http://talkbinary.com/programming/c/c-while-loop/">While Loops</a> and <a href="http://talkbinary.com/programming/c/c-for-loop/">For Loops</a> in practice due to their nature. They are only used for specific cases where the programmer needs the block of statements to be executed at least once. </p>
<h3 id="section-2">Going further with Do While Loops</h3>
<p>Now that you understand Do While Loops, <a href="http://talkbinary.com/programming/c/c-while-loop/">While Loops</a>, and <a href="http://talkbinary.com/programming/c/c-for-loop/">For Loops</a> why not try the following?</p>
<ol>
<li>Create a factorial program that uses a while, do while, and for loop. Do the same for summation. This should make you fully understand all loops and when to use them.</li>
</ol>
</li>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/programming/c/c-do-while-loop/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>C++ For Loop</title>
		<link>http://talkbinary.com/programming/c/c-for-loop/</link>
		<comments>http://talkbinary.com/programming/c/c-for-loop/#comments</comments>
		<pubDate>Sat, 14 Aug 2010 16:00:08 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[C++ For Loop]]></category>
		<category><![CDATA[C++ While Loop]]></category>
		<category><![CDATA[For Loop Break]]></category>
		<category><![CDATA[For Loop Example]]></category>
		<category><![CDATA[For Loop Factorial]]></category>
		<category><![CDATA[For Loop Summation]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=3170</guid>
		<description><![CDATA[In our previous tutorial, we learned about C++ While Loops. Today, we&#8217;ll learn about for loops. For loops are different than while loops due to the way they function. This allows one to choose what type of loop to use depending on the problem. For example, while loops are useful for executing statements until a<a class="moretag" href="http://talkbinary.com/programming/c/c-for-loop/">&#160;&#160;Full Article&#8230;</a>
]]></description>
				<content:encoded><![CDATA[<p>In our previous tutorial, we learned about C++ While Loops. Today, we&#8217;ll learn about for loops. </p>
<p>For loops are different than while loops due to the way they function. This allows one to choose what type of loop to use depending on the problem. For example, while loops are useful for executing statements until a condition is met and a for loop is useful when you know how many iterations you must complete before you get your result. </p>
<p>A for loop works as follows:</p>
<ol>
<li>The variable(s) is initialized</li>
<li>The condition is checked. If true, execute statements, else terminate for loop</li>
<li>Execute statements within for loop</li>
<li>Update variables and return to step B</li>
</ol>
<p><span id="more-3170"></span><br />
<center><div id="attachment_3176" class="wp-caption aligncenter" style="width: 310px"><a href="http://talkbinary.com/wp-content/uploads/2010/08/ForLoop.jpg"><img src="http://talkbinary.com/wp-content/uploads/2010/08/ForLoop-300x219.jpg" alt="For Loop" title="ForLoop" width="300" height="219" class="size-medium wp-image-3176" /></a><p class="wp-caption-text">Diagram of how a for loop works</p></div></center></p>
<p>The syntax is below:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span> initialization<span style="color: #008080;">;</span> condition<span style="color: #008080;">;</span> update<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
&nbsp;
<span style="color: #666666;">// statements </span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<h3 id="section-1">For Loop Example</h3>
<p>In our previous tutorial, we learned how to count forwards and backwards. In this tutorial, we&#8217;ll do something more interesting. We&#8217;ll compute the summation from 0 to <em>n</em>, n being user input. </p>
<p>To compute the summation, we simply add up all numbers starting from 0 to <em>n</em>. For example, the sum of 5 would be 0 + 1 + 2 + 3 + 4 + 5 = 15. Taking programming aside, the way we&#8217;d solve this step by step would be to have a running <em>sum</em> which we add to until we reach our desired <em>n</em>. Try programming this before you see the solution below.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> n <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>  <span style="color: #666666;">// User input</span>
&nbsp;
<span style="color: #0000ff;">int</span> sum <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> <span style="color: #666666;">// Our running sum</span>
&nbsp;
<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Sum: &quot;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> n<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// For loop declaration</span>
<span style="color: #666666;">// We use the variable i to denote an iteration</span>
<span style="color: #666666;">// In this example, we execute the following loop</span>
<span style="color: #666666;">// if i is less than or equal to the user input </span>
<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;=</span> n<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
&nbsp;
    sum <span style="color: #000040;">+</span><span style="color: #000080;">=</span> i<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>For example, if <em>n</em> was 3, the following loop would execute in the following manner.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;">i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> <span style="color: #0000dd;">0</span> <span style="color: #000080;">&lt;=</span> <span style="color: #0000dd;">3</span>
sum <span style="color: #000040;">+</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
sum <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span>
&nbsp;
i <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> <span style="color: #0000dd;">1</span> <span style="color: #000080;">&lt;=</span> <span style="color: #0000dd;">3</span>
sum <span style="color: #000040;">+</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
sum <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
&nbsp;
i <span style="color: #000080;">=</span> <span style="color: #0000dd;">2</span><span style="color: #008080;">;</span> <span style="color: #0000dd;">2</span> <span style="color: #000080;">&lt;=</span> <span style="color: #0000dd;">3</span>
sum <span style="color: #000040;">+</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">2</span>
sum <span style="color: #000080;">=</span> <span style="color: #0000dd;">3</span>
&nbsp;
i <span style="color: #000080;">=</span> <span style="color: #0000dd;">3</span><span style="color: #008080;">;</span> <span style="color: #0000dd;">3</span> <span style="color: #000080;">&lt;=</span> <span style="color: #0000dd;">3</span>
sum <span style="color: #000040;">+</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">3</span>
sum <span style="color: #000080;">=</span> <span style="color: #0000dd;">5</span>
&nbsp;
i <span style="color: #000080;">=</span> <span style="color: #0000dd;">4</span><span style="color: #008080;">;</span> <span style="color: #0000dd;">4</span> <span style="color: #000080;">&lt;=</span> <span style="color: #0000dd;">3</span>
<span style="color: #666666;">// End of For Loop</span></pre></td></tr></table></div>

<h3 id="section-2">For loops advanced</h3>
<p>Some things to know about for loops:</p>
<ul>
<li>You don&#8217;t have to declare a variable within the initialization block of a for loop.<br />
<blockquote>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> i<span style="color: #008080;">;</span>
<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> n<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span> <span style="color: #008000;">&#41;</span></pre></td></tr></table></div>

</blockquote>
</li>
<li>You may have multiple initializations, conditions, and updates if you use a comma in between them.<br />
<blockquote>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span>, j <span style="color: #000080;">=</span> <span style="color: #0000dd;">2</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> j<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span> <span style="color: #008000;">&#41;</span></pre></td></tr></table></div>

</blockquote>
</li>
<li>You can break out of a for loop using the following statement:<br />
<blockquote>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">break</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

</blockquote>
<p>This is useful for when you encounter a special case in your loop using if statements.</li>
<li>Likewise you may use <em>continue;</em> statement.</li>
</ul>
<h3 id="section-3">Going further with For Loops</h3>
<p>Now that you understand for loops, why not try the following?</p>
<ol>
<li>Write the previous for loop using a while loop. This will make you understand when to use a for loop</li>
<li>Write a for loop that computes the factorial of <em>n</em>.<br />
<blockquote><p>For example, if <em>n</em> is 3, then the factorial would be fac(3) = 1 x 2 x 3 = 6;</p></blockquote>
</li>
<li>Using two for loops (not nested), create a program that draws a triangle.<br />
<blockquote><p>For example, if <em>n</em> is 3, then the appropriate triangle would be </p>
<p>*<br />
**<br />
***<br />
**<br />
*</p></blockquote>
</li>
<li>Using a nested for loop, create a program that will draw a right triangle.<br />
<blockquote><p>For example, if <em>n</em> is 3, then the appropriate triangle would be</p>
<p>*<br />
**<br />
***</p></blockquote>
</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/programming/c/c-for-loop/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>C++ While Loop</title>
		<link>http://talkbinary.com/programming/c/c-while-loop/</link>
		<comments>http://talkbinary.com/programming/c/c-while-loop/#comments</comments>
		<pubDate>Fri, 13 Aug 2010 16:00:55 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[C++ While Loop]]></category>
		<category><![CDATA[counting program]]></category>
		<category><![CDATA[while loop]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=3155</guid>
		<description><![CDATA[In our previous tutorial, we learned about C++ If Statements which introduced us to the idea of control flow and conditions. Today, we introduce the while loop. The While Loop The while loop is another control flow structure that allows the programmer continuously execute a block of instructions until a specific condition is met. Below<a class="moretag" href="http://talkbinary.com/programming/c/c-while-loop/">&#160;&#160;Full Article&#8230;</a>
]]></description>
				<content:encoded><![CDATA[<p>In our previous tutorial, we learned about <a href="http://talkbinary.com/programming/c/c-if-else-statements/">C++ If Statements</a> which introduced us to the idea of control flow and conditions. Today, we introduce the while loop. </p>
<h3 id="section-1">The While Loop</h3>
<p>The while loop is another control flow structure that allows the programmer continuously execute a block of instructions until a specific condition is met. Below is an example of a program that contains a while loop. The idea of the program is for the user to input a variable n, and the while loop will continuously print out and increase n until it is over 100. </p>
<p><center><a href="http://talkbinary.com/wp-content/uploads/2010/08/WhileLoop.jpg"><img src="http://talkbinary.com/wp-content/uploads/2010/08/WhileLoop.jpg" alt="While Loop" title="WhileLoop" width="422" height="545" class="aligncenter size-full wp-image-3166" /></a></center></p>
<p>Now that you understand the diagram, take a look at the syntax.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span> condition <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> 
&nbsp;
     <span style="color: #666666;">// Statements </span>
&nbsp;
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>With the previous diagram, let&#8217;s make the program that increases and prints out n until it reaches 100. </p>
<p>In order to become a programmer, its essential that you learn how to create a program based on a specification. So its good to try it out on your own before you look at the solution below.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;">&nbsp;
<span style="color: #666666;">// Creating and initializing n</span>
<span style="color: #0000ff;">int</span> n <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// User prompt</span>
<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Please enter a number to count to 100: &quot;</span><span style="color: #008080;">;</span>
&nbsp;
&nbsp;
<span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> n<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// Beginning of while loop</span>
<span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span> n <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">100</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
&nbsp;
     <span style="color: #666666;">// Printing out n</span>
     <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> n <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">// Increasing n </span>
     n <span style="color: #000080;">=</span> n <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #008000;">&#125;</span> <span style="color: #666666;">// End of While Loop</span>
&nbsp;
<span style="color: #666666;">// Program continues</span></pre></td></tr></table></div>

<h3 id="section-2">Going further with While Loops</h3>
<p>Now that you learned how to create while loops, let&#8217;s have some more practice.</p>
<ol>
<li>Add to the previous program an if statement that determines if the user entered a negative number. If they did, output a message and set n to 0 so it only counts from a positive number to 100.</li>
<li>Make a while loop that prints a number from a user inputted n, to 0. If the number is below zero, let the user know, and set n to 100.</li>
<li>Using nested while loops, create a program that asks the user for a number from 0 &#8211; 10.
<p>Then print out n stars on one line and print out n number of lines. For example,</p>
<p>n = 3<br />
***<br />
***<br />
***<br />
n = 2<br />
**<br />
**</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/programming/c/c-while-loop/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>C++ If Else Statements</title>
		<link>http://talkbinary.com/programming/c/c-if-else-statements/</link>
		<comments>http://talkbinary.com/programming/c/c-if-else-statements/#comments</comments>
		<pubDate>Tue, 10 Aug 2010 00:07:22 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[boolean operators]]></category>
		<category><![CDATA[c++ if statement]]></category>
		<category><![CDATA[comparison operators]]></category>
		<category><![CDATA[control flow]]></category>
		<category><![CDATA[if else statement]]></category>
		<category><![CDATA[if statement]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=3058</guid>
		<description><![CDATA[In our previous tutorial, we learned about C++ input and output to make our programs more interactive. Well, today we are going to learn how to use If Then Else statements to allow us to control the flow of the program. This will allow us to execute code based on a given condition. Below is<a class="moretag" href="http://talkbinary.com/programming/c/c-if-else-statements/">&#160;&#160;Full Article&#8230;</a>
]]></description>
				<content:encoded><![CDATA[<p>In our previous tutorial, we learned about <a href="http://talkbinary.com/programming/c/c-input-and-output-cin-cout/">C++ input and outpu</a>t to make our programs more interactive. Well, today we are going to learn how to use If Then Else statements to allow us to control the flow of the program. This will allow us to execute code based on a given condition. Below is a simple example. </p>
<p><center><div id="attachment_3099" class="wp-caption aligncenter" style="width: 310px"><a href="http://talkbinary.com/wp-content/uploads/2010/08/IfStatementExamples.jpg"><img src="http://talkbinary.com/wp-content/uploads/2010/08/IfStatementExamples-300x206.jpg" alt="" title="IfStatementExamples" width="300" height="206" class="size-medium wp-image-3099" /></a><p class="wp-caption-text">Example of Control Flow </p></div></center></p>
<h3 id="section-1">Boolean Comparison Operators</h3>
<p>Before we teach you how to write these statements, we&#8217;ll show you the boolean comparison operators you may use for creating conditions. These condition statements will return either TRUE or FALSE which are useful in <em>if statements</em>.<br />
<span id="more-3058"></span></p>
<blockquote><table width="80%">
<tr>
<td><strong>Operator </strong></td>
<td><strong>Example</strong></td>
<td><strong>Description</strong></td>
</tr>
<tr>
<td>></td>
<td>a > b, a > 2, 4 > 2</td>
<td>Greater than</td>
</tr>
<tr>
<td>>=</td>
<td>a >=b, a >= 2, 4 >= 2</td>
<td>Greater than or equal to</td>
</tr>
<tr>
<td><</td>
<td>a < b, a < 2, 4 < 2</td>
<td>Less than</td>
</tr>
<tr>
<td><=</td>
<td>a <= b, a <=2, 4 <= 2</td>
<td>Less than or equal to</td>
</tr>
<tr>
<td>==</td>
<td>a == b, 2 == a</td>
<td>Equal to</td>
</tr>
<tr>
<td>!=</td>
<td>a != b, 2 != a</td>
<td>Does Not Equal to</td>
</tr>
</table>
</blockquote>
<p>You may use these boolean operators to compare data types of similar type. You may compare any of the <a href="http://talkbinary.com/programming/c/c-variables-and-data-types/">C++ Data Types</a> with each others whether they are literals such as 2, 3, &#8220;Hello&#8221;, or variables. If you compare mixed data types you will get different behavior. </p>
<p>Below are some examples of conditions. If they are literals, determine the conditions truth value.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000dd;">2</span> <span style="color: #000080;">&lt;</span> number
&nbsp;
<span style="color: #FF0000;">&quot;Hello&quot;</span> <span style="color: #000080;">==</span> <span style="color: #FF0000;">&quot;HELLO&quot;</span>
&nbsp;
<span style="color: #FF0000;">'a'</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #FF0000;">'A'</span>
&nbsp;
<span style="color:#800080;">2.0</span> <span style="color: #000080;">==</span> <span style="color: #0000dd;">2</span></pre></td></tr></table></div>

<p>You may also use the <strong>&#038;&#038;</strong> (and) and <strong>||</strong> (or) connectives in order to have multiple conditions. Below is an everyday example and after that some C++ examples.</p>
<p><center><a href="http://talkbinary.com/wp-content/uploads/2010/08/IfStatementExample2.jpg"><img src="http://talkbinary.com/wp-content/uploads/2010/08/IfStatementExample2-300x300.jpg" alt="Examples for both &amp;&amp; and ||" title="IfStatementExample2" width="300" height="300" class="aligncenter size-medium wp-image-3098" /></a></center></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;">&nbsp;
<span style="color: #666666;">// Returns true if both the 1st and 2nd conditions are true</span>
<span style="color: #0000dd;">2</span> <span style="color: #000080;">&lt;</span> a <span style="color: #000040;">&amp;&amp;</span> b <span style="color: #000080;">&gt;</span> c
&nbsp;
<span style="color: #666666;">// Returns true if either the 1st or 2nd conditions are true</span>
<span style="color: #0000dd;">2</span> <span style="color: #000080;">&lt;</span> a <span style="color: #000040;">||</span> b <span style="color: #000080;">&gt;</span> c</pre></td></tr></table></div>

<p>Finally, you may also use the <strong>!</strong> connective to make a condition have the opposite truth value. An example is below:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> a, b<span style="color: #008080;">;</span>
a <span style="color: #000080;">=</span> <span style="color: #0000dd;">2</span><span style="color: #008080;">;</span>
b <span style="color: #000080;">=</span> <span style="color: #0000dd;">3</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// The following would return TRUE</span>
a <span style="color: #000080;">&lt;</span> b 
&nbsp;
<span style="color: #666666;">// The following would return FALSE because the ! </span>
<span style="color: #666666;">// negates the truth value between the parenthesis</span>
<span style="color: #000040;">!</span> <span style="color: #008000;">&#40;</span> a <span style="color: #000080;">&lt;</span> b <span style="color: #008000;">&#41;</span></pre></td></tr></table></div>

<h3 id="section-2">C++ If Statement</h3>
<p>We will now start by showing you the syntax for writing an if statement.</p>
<blockquote><pre>
if ( condition ) { 

statements ...

}</pre>
</blockquote>
<p>Now, we&#8217;ll actually put it to some use. In our previous tutorial we learned about user input which will help us make things a lot more interesting. Let&#8217;s say we wanted to create a program that asks the user to guess a number and we&#8217;ll simply reply if it was the correct one. We&#8217;d do it in the following manner.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> magicNumber <span style="color: #000080;">=</span> <span style="color: #0000dd;">27</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> guess<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Guess my magic number: &quot;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> guess<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> magicNumber <span style="color: #000080;">==</span> guess <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
     <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Congratulations! Correct guess!&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>In the code above, the statement inside the if clause would execute if the condition were true. Therefore, if the user chooses our magic number <strong>27</strong>, our program will congratulate the user.</p>
<p><center><div id="attachment_3080" class="wp-caption aligncenter" style="width: 310px"><a href="http://talkbinary.com/wp-content/uploads/2010/08/IfStatement.jpg"><img src="http://talkbinary.com/wp-content/uploads/2010/08/IfStatement-300x206.jpg" alt="" title="IfStatement" width="300" height="206" class="size-medium wp-image-3080" /></a><p class="wp-caption-text">If Statement Diagram</p></div></center></p>
<h3 id="section-3">C++ If Else Statement</h3>
<p>Well, what if they didn&#8217;t? We would want to give them another message saying it was incorrect. Your first guess might be making another if statement but we can use the <em>else</em> statement. </p>
<blockquote><pre>
if ( condition ) { 

statements ...

} else { 

statements ...

}</pre>
</blockquote>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// .. Previous code</span>
&nbsp;
<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> magicNumber <span style="color: #000080;">==</span> guess <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
     <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Congratulations! Correct guess!&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span> 
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Incorrect!&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>In the previous code, if the user guess the number correctly, the program would congratulate the user. If the user did not guess the number correctly, it would display Incorrect!</p>
<p><center><div id="attachment_3079" class="wp-caption aligncenter" style="width: 310px"><a href="http://talkbinary.com/wp-content/uploads/2010/08/IfElseStatement.jpg"><img src="http://talkbinary.com/wp-content/uploads/2010/08/IfElseStatement-300x209.jpg" alt="" title="IfElseStatement" width="300" height="209" class="size-medium wp-image-3079" /></a><p class="wp-caption-text">If Else Statement</p></div></center></p>
<h3 id="section-4">C++ If Else If Else Statement</h3>
<p>Now, what if we wanted to actually let them know if their guess was too high or too low if it was incorrect? We can then use the <em>else if</em> statements. </p>
<blockquote><pre>
if ( condition ) { 

statements ...

} else if ( condition ) { 

statements ...

} else { 

statements ...

}</pre>
</blockquote>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// .. Previous code</span>
&nbsp;
<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> magicNumber <span style="color: #000080;">==</span> guess <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
     <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Congratulations! Correct guess!&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> magicNumber <span style="color: #000080;">&gt;</span> guess <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Your guess is too low!&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Your guess is too high!&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p><center><div id="attachment_3078" class="wp-caption aligncenter" style="width: 310px"><a href="http://talkbinary.com/wp-content/uploads/2010/08/IfElseIfElseStatement.jpg"><img src="http://talkbinary.com/wp-content/uploads/2010/08/IfElseIfElseStatement-300x256.jpg" alt="" title="IfElseIfElseStatement" width="300" height="256" class="size-medium wp-image-3078" /></a><p class="wp-caption-text">If Else Else If Statements</p></div></center></p>
<h3 id="section-5">Nesting If Statements</h3>
<p>You may also have nest if statements within if statements. An example is below.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> guess<span style="color: #008080;">;</span>
number <span style="color: #000080;">=</span> <span style="color: #0000dd;">25</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> number <span style="color: #000080;">==</span> guess <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
&nbsp;
     <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Now try guessing the second number!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
     <span style="color: #666666;">// Guess a second number</span>
     number <span style="color: #000080;">=</span> <span style="color: #0000dd;">56</span><span style="color: #008080;">;</span>
     <span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> guess<span style="color: #008080;">;</span>
&nbsp;
     <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> number <span style="color: #000080;">==</span> guess <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> 
         <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Congratulations! You guess two numbers correctly!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
     <span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span> 
         <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Try next time!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
     <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span> 
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Incorrect!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<h3 id="section-6">Overview of If Statements</h3>
<p>Some things to know about if statements are mentioned below.</p>
<ol>
<li>Remember, the <em>else</em> statement is optional and you may have zero, one, or more <em>else if</em> statements. </li>
<li>For good practice always indent or more formally known as tab your code when its within an <em>if</em> statement. That way it&#8217;s easy to follow. Imagine going over nested if statements that don&#8217;t have tabs? You would have a hard time figuring out which statements belong where.

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> a <span style="color: #000080;">&gt;</span> b <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> 
<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> c <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">2</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Yes!&quot;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span> 
<span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span> 
<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;No!&quot;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span> 
<span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span> 
<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;What?&quot;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

</li>
<li>If you only have one statement within an <em>if statement</em> you may omit the {}&#8217;s. For good practice, just keep them. It&#8217;s easier for someone else to go back to read and add to your code.

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> a <span style="color: #000080;">==</span> b <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> 
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Yes!&quot;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span> 
&nbsp;
<span style="color: #666666;">// Is the same as</span>
&nbsp;
<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> a <span style="color: #000080;">==</span> b <span style="color: #008000;">&#41;</span> 
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Yes!&quot;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

</li>
</ol>
<h3 id="section-7">Going further with If Statement</h3>
<p>Now that you know the basics of If Statements, why not do the following?</p>
<ol>
<li>Create an if statement that contains a condition that is always true.</li>
<li>Are &#8220;Hello&#8221; and &#8220;HELLO&#8221; equivalent? What about the char &#8216;a&#8217; and a string &#8220;a&#8221;?</li>
<li>Create a 3rd level deep if statement. The previous example shown was nested 2x.</li>
</ol>
<p>Keep patient, in our next tutorial we will learn about for loops and then while loops. Once we get a good handle of control flow, we can make some interesting programs.</p>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/programming/c/c-if-else-statements/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C++ Input and Output ( cin, cout )</title>
		<link>http://talkbinary.com/programming/c/c-input-and-output-cin-cout/</link>
		<comments>http://talkbinary.com/programming/c/c-input-and-output-cin-cout/#comments</comments>
		<pubDate>Thu, 05 Aug 2010 07:54:20 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[C++ cin]]></category>
		<category><![CDATA[C++ cout]]></category>
		<category><![CDATA[console]]></category>
		<category><![CDATA[input]]></category>
		<category><![CDATA[output]]></category>
		<category><![CDATA[standard input]]></category>
		<category><![CDATA[standard output]]></category>
		<category><![CDATA[terminal]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=3022</guid>
		<description><![CDATA[In our previous tutorial, we learned about the C++ String datatype. Until know, our C++ programs haven&#8217;t been that interactive. In this tutorial we are going to fix that. We are going to talk a little more in depth about gathering input from the user and displaying the output to the terminal. C++ Standard Output<a class="moretag" href="http://talkbinary.com/programming/c/c-input-and-output-cin-cout/">&#160;&#160;Full Article&#8230;</a>
]]></description>
				<content:encoded><![CDATA[<p>In our previous tutorial, we learned about the <a href="http://talkbinary.com/programming/c/c-string/">C++ String</a> datatype. Until know, our C++ programs haven&#8217;t been that interactive. In this tutorial we are going to fix that. We are going to talk a little more in depth about gathering input from the user and displaying the output to the terminal.</p>
<h3 id="section-1">C++ Standard Output</h3>
<p>If you recall, we have been using the <em>cout</em> object to display information on to the screen. Below, I&#8217;ll describe various ways to output strings, the contents of variables, expressions, and more.</p>
<h4 id="section-2">Basic output with endl</h4>
<p>Below is a simple example of printing out the string &#8220;Hello, World!&#8221; to the terminal accompanied by a newline.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// We first print out Hello World </span>
<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Hello, World!&quot;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// We then add the following to create a new line</span>
<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span></pre></td></tr></table></div>

<p><center><div id="attachment_3028" class="wp-caption alignnone" style="width: 310px"><a href="http://talkbinary.com/wp-content/uploads/2010/08/gvimandterminal.jpg"><img src="http://talkbinary.com/wp-content/uploads/2010/08/gvimandterminal-300x143.jpg" alt="" title="gvimandterminal" width="300" height="143" class="size-medium wp-image-3028" /></a><p class="wp-caption-text">The previous example is demonstrated above.</p></div></center></p>
<h4 id="section-3">Basic output with variables</h4>
<p>Now, we are going to display the contents of a variable. Notice how we can output more than one item with a single <em>cout</em> object.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> num <span style="color: #000080;">=</span> <span style="color: #0000dd;">5</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> num <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span></pre></td></tr></table></div>

<h4 id="section-4">Basic output with math expressions</h4>
<p>We can also output arithmetic expressions. This by the way, is something you will hardly ever use but its good to know anyways since you&#8217;ll always want to save the result of an expression in a variable.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #0000dd;">5</span> <span style="color: #000040;">*</span> <span style="color: #0000dd;">4</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">3</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span></pre></td></tr></table></div>

<h4 id="section-5">Basic output with boolean expressions</h4>
<p>Finally, we will demonstrate a simple example with a boolean expression. This will not print out <em>true</em> or <em>false</em>. It will print out either a <em>1</em> for true, or <em>0</em> for false. You also need the parenthesis, else the < operator will give you an error.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span> <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">3</span><span style="color: #008000;">&#41;</span>  <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span></pre></td></tr></table></div>

<h3 id="section-6">C++ Standard Input</h3>
<p>For standard output we used the object <em>cout</em> and for standard input, we will use the object <em>cin</em>. Before we start with examples, there is are a couple of things you must know. </p>
<blockquote><ul>
<li>User input is usually done through the keyboard and the terminal. Your program will halt at each user input and keep executing when the user enters a value and then presses enter</li>
<li>The <em>cin</em> object will read in the type of object you tell it to. Therefore, if you wish to have a char, it will read a char. Same with an integer, float, and a string. If you mix things up, you will receive undesired behavior</li>
<li>The user might provide you with invalid values and therefore require you to do error checking. We will leave this for a later topic.</li>
</ul>
</blockquote>
<h4 id="section-7">Reading in a value</h4>
<p>Below is an example of reading in a single and multiple values. Try it by changing the object type.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// We declare and initialize the variable num to -1</span>
<span style="color: #0000ff;">int</span> num <span style="color: #000080;">=</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// We prompt the user for input</span>
<span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> num<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// We declare three strings</span>
string a, b, c<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// We read three strings from input</span>
<span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> a <span style="color: #000080;">&gt;&gt;</span> b <span style="color: #000080;">&gt;&gt;</span> c<span style="color: #008080;">;</span></pre></td></tr></table></div>

<h3 id="section-8">Going further with input and output</h3>
<p>Now that you understand the basics of input and output why not do the following?</p>
<ol>
<li>Prompt the user for his First Name, Last Name, Age, City, and State in the following fashion.</p>
<pre lang="">
Enter your first name:
Enter your last name:
Enter your age:
Enter your city:
Enter your state:</pre>
<p><br/><br />
Then, output the results in the following fashion. </p>
<pre lang="">
Hello <strong>firstname lastname</strong>! 
I see you are <strong>age</strong> years old and live in <strong>city</strong>, <strong>state</strong>.</pre>
</li>
<li>Now try declaring a variable without initializing it. Then output it. What happened?</li>
<li>Try reading in an int value, but give it a string. Output the variable and what happens?</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/programming/c/c-input-and-output-cin-cout/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>C++ String</title>
		<link>http://talkbinary.com/programming/c/c-string/</link>
		<comments>http://talkbinary.com/programming/c/c-string/#comments</comments>
		<pubDate>Tue, 03 Aug 2010 16:00:46 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[C++ string]]></category>
		<category><![CDATA[string class]]></category>
		<category><![CDATA[string concatenate]]></category>
		<category><![CDATA[string substr]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=2965</guid>
		<description><![CDATA[In our previous tutorial we discussed C++ Variables and Data Types, today, we are going to introduce a new data type, the string class. C++ String The string class represents what you may know as a string of text. Remember our previous C++ Hello World program? Well, you can also think of the output to<a class="moretag" href="http://talkbinary.com/programming/c/c-string/">&#160;&#160;Full Article&#8230;</a>
]]></description>
				<content:encoded><![CDATA[<p>In our previous tutorial we discussed <a href="http://talkbinary.com/programming/c/c-variables-and-data-types/">C++ Variables and Data Types</a>, today, we are going to introduce a new data type, the string class. </p>
<h3 id="section-1">C++ String</h3>
<p>The string class represents what you may know as a string of text. Remember our previous <a href="http://talkbinary.com/programming/c/c-hello-world/">C++ Hello World</a> program? Well, you can also think of the output to the terminal as a string of text.<br />
<span id="more-2965"></span><br />
Below is some simple usage of the string class:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;iostream&gt;</span>
&nbsp;
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> 
&nbsp;
    string message<span style="color: #008080;">;</span>
    message <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;Hello World!&quot;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> message <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>The output of course, would be a simple &#8220;Hello World!&#8221; to the console ending with a newline. </p>
<h3 id="section-2">String class functions</h3>
<p>Until now, we haven&#8217;t discussed much about functions. We&#8217;ll discuss them in more detail later, but for now just know that functions provide us more functionality with a data type. We&#8217;ll only be discussing functions that either modify or access more information about a data type.</p>
<blockquote><p>For example, think of it as a black box. You provide it an object, lets say a car, and you provide some input such as color of the car, type of engine you want, and other options. Well, this function (our black box) would return a car modified to your inputs. </p>
<p>The beauty of functions, is that you don&#8217;t need to know what happens internally! You simply provide the object and inputs if necessary, and you should receive your desired output. We&#8217;ll discuss later how to create our own functions.
</p></blockquote>
<p>In order to call a function, you simply specify the <em>identifier</em> , <em>function</em> name, and <em>input parameters</em> if necessary. Follow the examples provided below and you&#8217;ll understand. We&#8217;ll be continuing the previous usage example, so feel free to keep adding to your program.</p>
<p>For the following functions, take into consideration how the string is stored. This will help you in understanding the following functions.</p>
<p><center><div id="attachment_3017" class="wp-caption aligncenter" style="width: 310px"><a href="http://talkbinary.com/wp-content/uploads/2010/08/String1.jpg"><img src="http://talkbinary.com/wp-content/uploads/2010/08/String1-300x64.jpg" alt="" title="String" width="300" height="64" class="size-medium wp-image-3017" /></a><p class="wp-caption-text">The image above displays the indices of the characters starting at position 0. </p></div></center></p>
<h4 id="section-3">length()</h4>
<table border="1" color="#E0E0E0" >
<tr>
<td width="100px">length</td>
<td width="350px">Returns the size of a string in characters</td>
</tr>
</table>
<p><br/></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// Stores the size of our message in an int value</span>
<span style="color: #0000ff;">int</span> size <span style="color: #000080;">=</span> message.<span style="color: #007788;">length</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// The result will be 12. A space is counted as a character.</span>
<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> size <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span></pre></td></tr></table></div>

<h4 id="section-4">substr()</h4>
<table border="1" color="#E0E0E0" >
<tr>
<td width="100px">substr</td>
<td width="350px">Generates a substring based on two input parameters</td>
</tr>
</table>
<p><br/></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// Generates a substring of size 5, starting at position 2</span>
string substring <span style="color: #000080;">=</span> message.<span style="color: #007788;">substr</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">2</span>,<span style="color: #0000dd;">5</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// The result is &quot;llo W&quot;</span>
<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> substring <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span></pre></td></tr></table></div>

<h4 id="section-5">operator+</h4>
<table border="1" color="#E0E0E0" >
<tr>
<td width="100px">+</td>
<td width="350px">Concatenate two strings together</td>
</tr>
</table>
<p><br/></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;">string a <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;Hello &quot;</span><span style="color: #008080;">;</span>
string b <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;World!&quot;</span><span style="color: #008080;">;</span>
&nbsp;
string c <span style="color: #000080;">=</span> a <span style="color: #000040;">+</span> b<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// Will display &quot;Hello World!&quot;;</span>
<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> c <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>The best way to learn how to use strings, is by using them.</p>
<h3 id="section-6">Going further with the String class</h3>
<p>Now that you understand the basics of strings why not do the following? </p>
<ol>
<li>Develop a program that contains your first and last name in two respective variables called <em>first</em> and <em>last</em>. </p>
<p>Concatenate your first and last name to create another variable, <em>fullname</em>.</p>
<p>Now, using the substr function, separate both your first and last name from the <em>fullname</em> variable.</p>
<p><em>For example this should be the output:</em></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;">First name<span style="color: #008000;">&#40;</span>first<span style="color: #008000;">&#41;</span><span style="color: #008080;">:</span> Diego
Last name<span style="color: #008000;">&#40;</span>last<span style="color: #008000;">&#41;</span><span style="color: #008080;">:</span> Villasenor
Full name<span style="color: #008000;">&#40;</span>fullname<span style="color: #008000;">&#41;</span><span style="color: #008080;">:</span> Diego Villasenor
&nbsp;
First <span style="color: #0000ff;">using</span> substr<span style="color: #008000;">&#40;</span>fullname<span style="color: #008000;">&#41;</span><span style="color: #008080;">:</span> Diego
Last <span style="color: #0000ff;">using</span> substr<span style="color: #008000;">&#40;</span>fullname<span style="color: #008000;">&#41;</span><span style="color: #008080;">:</span> Villasenor</pre></td></tr></table></div>

</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/programming/c/c-string/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C++ Variables and Data Types</title>
		<link>http://talkbinary.com/programming/c/c-variables-and-data-types/</link>
		<comments>http://talkbinary.com/programming/c/c-variables-and-data-types/#comments</comments>
		<pubDate>Fri, 30 Jul 2010 18:22:57 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[C++ bool]]></category>
		<category><![CDATA[C++ char]]></category>
		<category><![CDATA[C++ data types]]></category>
		<category><![CDATA[C++ double]]></category>
		<category><![CDATA[C++ float]]></category>
		<category><![CDATA[C++ identifiers]]></category>
		<category><![CDATA[C++ int]]></category>
		<category><![CDATA[C++ variables]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=2912</guid>
		<description><![CDATA[In our previous C++ Hello World tutorial, we learned how to output &#8220;Hello World&#8221; to the terminal. Now, we are going to learn how to create variables that contain different types of information in memory so we can manipulate these variables throughout our program. C++ Variables Variables are created through the use of identifiers. An<a class="moretag" href="http://talkbinary.com/programming/c/c-variables-and-data-types/">&#160;&#160;Full Article&#8230;</a>
]]></description>
				<content:encoded><![CDATA[<p>In our previous <a href="http://talkbinary.com/programming/c/c-hello-world/">C++ Hello World</a> tutorial, we learned how to output &#8220;Hello World&#8221; to the terminal. Now, we are going to learn how to create variables that contain different types of information in memory so we can manipulate these variables throughout our program.</p>
<h3 id="section-1">C++ Variables</h3>
<p>Variables are created through the use of identifiers. An <em>identifier</em> is a sequence of one or more digits, letters, or the underscore character. It also may not be a reserved word, have two consecutive underscores, or begin with a digit. A reserved word is simply a keyword that is reserved by the language such as: int, double, string.</p>
<p>Below, is an example of the creation and manipulation of variables.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// Below we are creating two different variables of type int</span>
<span style="color: #0000ff;">int</span> num <span style="color: #000080;">=</span> <span style="color: #0000dd;">5</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">int</span> num2 <span style="color: #000080;">=</span> <span style="color: #0000dd;">10</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// Below we are creating a variable of type in that stores the sum of the </span>
<span style="color: #666666;">// previous two variables.</span>
<span style="color: #0000ff;">int</span> sum <span style="color: #000080;">=</span> num <span style="color: #000040;">+</span> num2<span style="color: #008080;">;</span></pre></td></tr></table></div>

<p><span id="more-2912"></span></p>
<h3 id="section-2">C++ Fundamental Data Types</h3>
<p>For now, we will discuss a couple of the fundamental data types the C++ language has to offer. Later, we&#8217;ll learn how to create our own through the use of classes.</p>
<table id="hor-minimalist-a">
<thead>
<tr>
<th scope="col">Variable</th>
<th scope="col">Example</th>
<th scope="col">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>int</td>
<td>1, 2, 10, 999, -562</td>
<td>A positive or negative integer value</td>
</tr>
<tr>
<td>double</td>
<td>1, 1.2, -1.2</td>
<td>A floating-point variable</td>
</tr>
<tr>
<td>bool</td>
<td>true, false</td>
<td>Either true or false, useful for conditions</td>
</tr>
<tr>
<td>char</td>
<td>a, b, 2, 1</td>
<td>A single character</td>
</tr>
</tbody>
</table>
<h3 id="section-3">Declaring variables</h3>
<p>In order to declare a variable, you need to specify the type of variable, identifier, and initialize it if you wish. Below are a couple of examples.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// Declaring and initializing variables differently</span>
<span style="color: #0000ff;">int</span> num1 <span style="color: #000080;">=</span> <span style="color: #0000dd;">5</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">double</span> num2<span style="color: #008080;">;</span>
num2 <span style="color: #000080;">=</span> <span style="color:#800080;">2.5</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// Declaring a list of variables</span>
<span style="color: #0000ff;">int</span> a, b, c, d<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// Setting a boolean value</span>
<span style="color: #0000ff;">bool</span> val <span style="color: #000080;">=</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// When you declare a character, you need to use the ''s</span>
<span style="color: #0000ff;">char</span> e <span style="color: #000080;">=</span> <span style="color: #FF0000;">'b'</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>Some things to take into consideration are below:</p>
<ul>
<li>Uninitialized variables is a bad practice. If you forget, and try to use them later throughout your program they will contain garbage and will lead to undefined behavior for your program</li>
</ul>
<h3 id="section-4">Example of C++ Program with Variables</h3>
<p>Below is a simple example of a program that uses what we just learned. Try it out for yourself and try new things as well.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;iostream&gt;</span>
&nbsp;
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> 
&nbsp;
     <span style="color: #666666;">//Outputs to the terminal, followed by a newline character</span>
     <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Welcome!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
&nbsp;
     <span style="color: #666666;">// Declaring some variables and initializing them</span>
     <span style="color: #0000ff;">int</span> a, b, c<span style="color: #008080;">;</span>
     a <span style="color: #000080;">=</span> <span style="color: #0000dd;">3</span><span style="color: #008080;">;</span>
     b <span style="color: #000080;">=</span> <span style="color: #0000dd;">2</span><span style="color: #008080;">;</span>
     c <span style="color: #000080;">=</span> a <span style="color: #000040;">+</span> b<span style="color: #008080;">;</span>
&nbsp;
     <span style="color: #666666;">// Outputting c to the terminal as well as a new line</span>
     <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;The sum of a and b is: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> c <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
     <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<h3 id="section-5">Further practice</h3>
<p>You need further practice? Don&#8217;t worry, in the next few tutorials, we&#8217;ll learn how to use the different variable types you learned today. Just as long as you understand the material in this tutorial you are doing fine. Have any questions? Post them below.</p>
<p>In the next tutorial, we&#8217;ll be learning about global and constant variables as well as the scope of variables.</p>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/programming/c/c-variables-and-data-types/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced

 Served from: talkbinary.com @ 2013-05-23 04:47:36 by W3 Total Cache -->