<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: 0.7 &#8212; A few common C++ problems</title>
	<atom:link href="http://www.learncpp.com/cpp-tutorial/07-a-few-common-cpp-problems/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.learncpp.com/cpp-tutorial/07-a-few-common-cpp-problems/</link>
	<description></description>
	<pubDate>Sun, 20 Jul 2008 14:26:51 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
		<item>
		<title>By: Joyel</title>
		<link>http://www.learncpp.com/cpp-tutorial/07-a-few-common-cpp-problems/#comment-19941</link>
		<dc:creator>Joyel</dc:creator>
		<pubDate>Sat, 28 Jun 2008 01:45:10 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/07-a-few-common-c-compilingexecuting-problems/#comment-19941</guid>
		<description>I find some of these very helpful..thanks.</description>
		<content:encoded><![CDATA[<p>I find some of these very helpful..thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris</title>
		<link>http://www.learncpp.com/cpp-tutorial/07-a-few-common-cpp-problems/#comment-18188</link>
		<dc:creator>Chris</dc:creator>
		<pubDate>Wed, 04 Jun 2008 01:19:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/07-a-few-common-c-compilingexecuting-problems/#comment-18188</guid>
		<description>I've been learning C++ for a while now but you can also put this code at the end of your file and before return 0; in the main function to keep it running and then enter y or n to quit or not, respectively:

&lt;pre&gt;
char quitKey = 'y';
do
    {
        cout &#60;&#60; quitKey;
    }
    while (quitKey != 'y');
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>I&#8217;ve been learning C++ for a while now but you can also put this code at the end of your file and before return 0; in the main function to keep it running and then enter y or n to quit or not, respectively:</p>
<pre>
char quitKey = 'y';
do
    {
        cout &lt;&lt; quitKey;
    }
    while (quitKey != 'y');
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Uiii</title>
		<link>http://www.learncpp.com/cpp-tutorial/07-a-few-common-cpp-problems/#comment-10363</link>
		<dc:creator>Uiii</dc:creator>
		<pubDate>Tue, 25 Mar 2008 22:04:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/07-a-few-common-c-compilingexecuting-problems/#comment-10363</guid>
		<description>If you're using Dev-C++, you can put on the end of main function (before return 0;) system("PAUSE"); to don't close console window immediately. This command pause the program and wait to keypress. But I think that it works only in MS Windows.</description>
		<content:encoded><![CDATA[<p>If you&#8217;re using Dev-C++, you can put on the end of main function (before return 0;) system(&#8221;PAUSE&#8221;); to don&#8217;t close console window immediately. This command pause the program and wait to keypress. But I think that it works only in MS Windows.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Learn C++ - &#187; 0.6 -- Compiling your first program</title>
		<link>http://www.learncpp.com/cpp-tutorial/07-a-few-common-cpp-problems/#comment-8246</link>
		<dc:creator>Learn C++ - &#187; 0.6 -- Compiling your first program</dc:creator>
		<pubDate>Thu, 21 Feb 2008 00:29:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/07-a-few-common-c-compilingexecuting-problems/#comment-8246</guid>
		<description>[...] This is an issue with some compilers, such as Bloodshed&#8217;s Dev-C++. We present a solution to this problem in lesson 0.7 &#8212; a few common cpp problems. [...]</description>
		<content:encoded><![CDATA[<p>[...] This is an issue with some compilers, such as Bloodshed&#8217;s Dev-C++. We present a solution to this problem in lesson 0.7 &#8212; a few common cpp problems. [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Timon</title>
		<link>http://www.learncpp.com/cpp-tutorial/07-a-few-common-cpp-problems/#comment-4843</link>
		<dc:creator>Timon</dc:creator>
		<pubDate>Sun, 23 Dec 2007 11:42:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/07-a-few-common-c-compilingexecuting-problems/#comment-4843</guid>
		<description>I made a comment here about endl, with a different name. Tiomon.. the infamous typo..

I kept getting an error because instead of endl (lowercase L) I put end1 (ONE). I think you should put this up there since even though it's humiliating I took a "little" while trying to fix it (thanks to Google..) someone else might encounter the same thing I did.

[ That's a great point.  I'll add it to the FAQ.  I added some additional advice  in the answer to this issue. -Alex ]</description>
		<content:encoded><![CDATA[<p>I made a comment here about endl, with a different name. Tiomon.. the infamous typo..</p>
<p>I kept getting an error because instead of endl (lowercase L) I put end1 (ONE). I think you should put this up there since even though it&#8217;s humiliating I took a &#8220;little&#8221; while trying to fix it (thanks to Google..) someone else might encounter the same thing I did.</p>
<p>[ That's a great point.  I'll add it to the FAQ.  I added some additional advice  in the answer to this issue. -Alex ]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jason</title>
		<link>http://www.learncpp.com/cpp-tutorial/07-a-few-common-cpp-problems/#comment-4633</link>
		<dc:creator>Jason</dc:creator>
		<pubDate>Tue, 18 Dec 2007 23:47:25 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/07-a-few-common-c-compilingexecuting-problems/#comment-4633</guid>
		<description>That helps a lot.
Very cool.</description>
		<content:encoded><![CDATA[<p>That helps a lot.<br />
Very cool.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex</title>
		<link>http://www.learncpp.com/cpp-tutorial/07-a-few-common-cpp-problems/#comment-4631</link>
		<dc:creator>Alex</dc:creator>
		<pubDate>Tue, 18 Dec 2007 22:28:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/07-a-few-common-c-compilingexecuting-problems/#comment-4631</guid>
		<description>Jason, good question.  Here's a simple program that illustrates why &lt;code&gt;cin &gt;&gt; myVariable&lt;/code&gt; is not the best way to pause:

&lt;pre&gt;
#include &#60;iostream&#62;

int main()
{ 
    using namespace std;

    // Allocate a buffer to hold user input
    char buf[255] = { 0 };

    // We only want one letter
    cout &#60;&#60; &#34;Enter a letter: &#34;;
    cin &#62;&#62; buf[0];

    // Pause and wait for user input
    char chIgnore;
    cout &#60;&#60; &#34;Pausing&#34; &#60;&#60; endl;
    cin &#62;&#62; chIgnore;

  return 0;
}
&lt;/pre&gt;

First, &lt;code&gt;cin &gt;&gt; chIgnore&lt;/code&gt; ignores the enter key if the user does not provide any other input.  You can verify this by hitting enter when the &lt;code&gt;cin &gt;&gt; chIgnore&lt;/code&gt; line is being executed.  On the other hand, cin.get() accepts the enter key as valid input.

But perhaps more importantly is the second phenomena that occurs when more characters are placed into the input stream (cin) than are read out.  When asked for a letter in the above program, enter more than one letter and watch what happens.

Let's say you enter "abcde".  &lt;code&gt;cin &gt;&gt; buf[0]&lt;/code&gt; reads the 'a' character, but "bcde" are still left in the input stream.  Consequently, when the code gets to &lt;code&gt;cin &gt;&gt; chIgnore&lt;/code&gt;, it reads the waiting 'b' character out of the input stream and doesn't pause at all!

The 3 step process is a little more foolproof.  First, &lt;code&gt;cin.clear()&lt;/code&gt; clears any errors the user may have caused by providing invalid input in the past.  Second, &lt;code&gt;cin.ignore(255, 'n')&lt;/code&gt; gets rid of up to 255 waiting characters in the input stream (or until a n is encountered), which is almost always going to be enough to clear out the input stream.  Finally, we use &lt;code&gt;cin.get()&lt;/code&gt; to wait for a character.  Since we just cleared the input stream, there are no characters waiting.  Consequently, the program will wait for the user to enter something.

In 99% of the cases you encounter, &lt;code&gt;cin &gt;&gt; myVariable&lt;/code&gt; will probably work exactly as you expect.  But since the 3 line method is generally more foolproof and isn't particularly burdensome to implement, I'd say it's the superior solution.</description>
		<content:encoded><![CDATA[<p>Jason, good question.  Here&#8217;s a simple program that illustrates why <code>cin >> myVariable</code> is not the best way to pause:</p>
<pre>
#include &lt;iostream&gt;

int main()
{
    using namespace std;

    // Allocate a buffer to hold user input
    char buf[255] = { 0 };

    // We only want one letter
    cout &lt;&lt; &quot;Enter a letter: &quot;;
    cin &gt;&gt; buf[0];

    // Pause and wait for user input
    char chIgnore;
    cout &lt;&lt; &quot;Pausing&quot; &lt;&lt; endl;
    cin &gt;&gt; chIgnore;

  return 0;
}
</pre>
<p>First, <code>cin >> chIgnore</code> ignores the enter key if the user does not provide any other input.  You can verify this by hitting enter when the <code>cin >> chIgnore</code> line is being executed.  On the other hand, cin.get() accepts the enter key as valid input.</p>
<p>But perhaps more importantly is the second phenomena that occurs when more characters are placed into the input stream (cin) than are read out.  When asked for a letter in the above program, enter more than one letter and watch what happens.</p>
<p>Let&#8217;s say you enter &#8220;abcde&#8221;.  <code>cin >> buf[0]</code> reads the &#8216;a&#8217; character, but &#8220;bcde&#8221; are still left in the input stream.  Consequently, when the code gets to <code>cin >> chIgnore</code>, it reads the waiting &#8216;b&#8217; character out of the input stream and doesn&#8217;t pause at all!</p>
<p>The 3 step process is a little more foolproof.  First, <code>cin.clear()</code> clears any errors the user may have caused by providing invalid input in the past.  Second, <code>cin.ignore(255, 'n')</code> gets rid of up to 255 waiting characters in the input stream (or until a n is encountered), which is almost always going to be enough to clear out the input stream.  Finally, we use <code>cin.get()</code> to wait for a character.  Since we just cleared the input stream, there are no characters waiting.  Consequently, the program will wait for the user to enter something.</p>
<p>In 99% of the cases you encounter, <code>cin >> myVariable</code> will probably work exactly as you expect.  But since the 3 line method is generally more foolproof and isn&#8217;t particularly burdensome to implement, I&#8217;d say it&#8217;s the superior solution.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jason</title>
		<link>http://www.learncpp.com/cpp-tutorial/07-a-few-common-cpp-problems/#comment-4630</link>
		<dc:creator>Jason</dc:creator>
		<pubDate>Tue, 18 Dec 2007 22:10:44 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/07-a-few-common-c-compilingexecuting-problems/#comment-4630</guid>
		<description>In order to get the screen to pause before exiting the program, does the code:
&lt;pre&gt;
cin.clear();   
cin.ignore(255, '\n');   
cin.get();
&lt;/pre&gt;  
have any advantages over just using 
&lt;pre&gt;
cin &gt;&gt; myVariable;
&lt;/pre&gt;
I have so much to learn. :)</description>
		<content:encoded><![CDATA[<p>In order to get the screen to pause before exiting the program, does the code:</p>
<pre>
cin.clear();
cin.ignore(255, '\n');
cin.get();
</pre>
<p>have any advantages over just using </p>
<pre>
cin >> myVariable;
</pre>
<p>I have so much to learn. :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: foq</title>
		<link>http://www.learncpp.com/cpp-tutorial/07-a-few-common-cpp-problems/#comment-4562</link>
		<dc:creator>foq</dc:creator>
		<pubDate>Mon, 17 Dec 2007 15:25:49 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/07-a-few-common-c-compilingexecuting-problems/#comment-4562</guid>
		<description>Another great way to get a program to pause is to require a user input (cin).  Using a menu-type interface that requires the user to select an option for "exiting" works well.  I'm only a beginner, but for simple calculators that I've created, this works well.</description>
		<content:encoded><![CDATA[<p>Another great way to get a program to pause is to require a user input (cin).  Using a menu-type interface that requires the user to select an option for &#8220;exiting&#8221; works well.  I&#8217;m only a beginner, but for simple calculators that I&#8217;ve created, this works well.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jason</title>
		<link>http://www.learncpp.com/cpp-tutorial/07-a-few-common-cpp-problems/#comment-4554</link>
		<dc:creator>Jason</dc:creator>
		<pubDate>Mon, 17 Dec 2007 10:22:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/07-a-few-common-c-compilingexecuting-problems/#comment-4554</guid>
		<description>Problem 1: When executing a program from the IDE, the console window blinks and then closes immediately.

The ide I'm using is Visual C   .NET 2002.  This ide has problem 1.

To fix the problem I include the following command at the end of the main() function before the return statement.

system("pause");

[ Unfortunately, this only works on the Windows operating system.  The system() function tells the operating system to execute whatever the parameter is -- in this case, a command named "pause".  On Windows, this waits for user input.  On other operating systems (such as linux), the pause command does something else entirely.  However, if you are only programming on Windows and not distributing your source code, you may find this an easier solution while learning. -Alex ] </description>
		<content:encoded><![CDATA[<p>Problem 1: When executing a program from the IDE, the console window blinks and then closes immediately.</p>
<p>The ide I&#8217;m using is Visual C   .NET 2002.  This ide has problem 1.</p>
<p>To fix the problem I include the following command at the end of the main() function before the return statement.</p>
<p>system(&#8221;pause&#8221;);</p>
<p>[ Unfortunately, this only works on the Windows operating system.  The system() function tells the operating system to execute whatever the parameter is -- in this case, a command named "pause".  On Windows, this waits for user input.  On other operating systems (such as linux), the pause command does something else entirely.  However, if you are only programming on Windows and not distributing your source code, you may find this an easier solution while learning. -Alex ]</p>
]]></content:encoded>
	</item>
</channel>
</rss>
