<?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/"
	xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"
>

<channel>
	<title>Wazoo Enterprises<title>&#187; our books</title>
</title>
	<atom:link href="http://www.wazooinc.com/category/our-books/feed" rel="self" type="application/rss+xml" />
	<link>http://www.wazooinc.com</link>
	<description>making software fun again</description>
	<lastBuildDate>Thu, 09 Sep 2010 06:04:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
<!-- podcast_generator="Blubrry PowerPress/1.0.5" mode="simple" entry="normal" -->
	<itunes:summary>making software fun again</itunes:summary>
	<itunes:author>Wazoo Enterprises</itunes:author>
	<itunes:explicit>no</itunes:explicit>
	<itunes:image href="http://www.wazooinc.com/wp-content/plugins/powerpress/itunes_default.jpg" />
	<itunes:owner>
		<itunes:name>Wazoo Enterprises</itunes:name>
		<itunes:email>erikyuzwa@gmail.com</itunes:email>
	</itunes:owner>
	<managingEditor>erikyuzwa@gmail.com (Wazoo Enterprises)</managingEditor>
	<copyright>Copyright &#xA9; Wazoo Enterprises 2010</copyright>
	<itunes:subtitle>making software fun again</itunes:subtitle>
	<image>
		<title>Wazoo Enterprises&lt;title&gt;&#187; our books&lt;/title&gt;
</title>
		<url>http://www.wazooinc.com/wp-content/plugins/powerpress/rss_default.jpg</url>
		<link>http://www.wazooinc.com/category/our-books</link>
	</image>
<cloud domain='www.wazooinc.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>discovering a bug with the cin and cout streams</title>
		<link>http://www.wazooinc.com/discovering-a-bug-with-the-cin-and-cout-streams.html</link>
		<comments>http://www.wazooinc.com/discovering-a-bug-with-the-cin-and-cout-streams.html#comments</comments>
		<pubDate>Tue, 12 Jan 2010 07:48:35 +0000</pubDate>
		<dc:creator>wazoo</dc:creator>
				<category><![CDATA[our books]]></category>
		<category><![CDATA[book support]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[learn c++ by making games]]></category>

		<guid isPermaLink="false">http://www.wazooinc.com/?p=341</guid>
		<description><![CDATA[A reader had sent in the following request for help, from my section in the material which deals with cin, cout and basic serialization using C++: using bloodshed and your code, Listing 4.1 (pg 46 &#8211; 48)in Learning C++ by Making Games compiles well but skips the input stage for amorphouseObject. It asks the question [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.wazooinc.com%2Fdiscovering-a-bug-with-the-cin-and-cout-streams.html"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.wazooinc.com%2Fdiscovering-a-bug-with-the-cin-and-cout-streams.html&amp;source=wazooinc&amp;style=normal&amp;service=bit.ly&amp;service_api=headmaster%3AR_9643738fb640817d9b7ff248d89f8c7e" height="61" width="50" /><br />
			</a>
		</div>
<p>A reader had sent in the following request for help, from my section in the material which deals with cin, cout and basic serialization using C++:</p>
<blockquote><p>
using bloodshed and your code, Listing 4.1 (pg 46 &#8211; 48)in Learning C++ by<br />
Making Games compiles well but skips the input stage for amorphouseObject.<br />
It asks the question (cout) but does not wait for the answer before it asks<br />
the next question.  It also did it for my students with their code. Any<br />
ideas why?</p>
<p>Thanks!
</p></blockquote>
<p>I sat down and went through the sample code to see what was happening. Sure enough, midway through the example, it appeared as if an extra return character was somehow getting into the cin input stream and mucking up some of the input. For a quick recap, let&#8217;s look at what was originally posted:</p>
<pre><code>
/*************************************
 Learn C++ Programming by Making Games
 Project 4.1 - Game of Funny Headlines
*************************************/

#include <iostream>
#include <string>
using namespace std;

int main()
{
  // First, let's welcome the user
  cout << "Welcome to the C++ News Network!" << endl << endl;

  // Then, let's input several values to plug into our headlines...
  // Note that the questions don't always match the names of the
  // variables because we are trying to surprise the player.

  string userName;
  cout << "Please type in your first name: " << endl;
  cin >> userName;

  int smallNumber;
  cout << "How many siblings do you have?" << endl;
  cin >> smallNumber;

  float largeNumber;
  cout << "How much money would you like to earn every year?" << endl;
  cin >> largeNumber;

  string color;
  cout << "Tell us your least favourite color:" << endl;
  cin >> color;

  string amorphousObject;
  cout << "Which vegetables have the weirdest shapes?" << endl;
  getline( cin, amorphousObject );

  string deadGuy;
  cout << "Name a famous dead person:" << endl;
  getline( cin, deadGuy );

  string celebrityActor;
  cout << "Who is your favorite actor?" << endl;
  getline( cin, celebrityActor );

  string politician;
  cout << "Name a current world leader:" << endl;
  getline( cin, politician );

  string cartoonCharacter;
  cout << "Who is your favourite cartoon character?" << endl;
  getline( cin, cartoonCharacter );

  string weirdGroup;
  cout << "Name a hobby or a profession you find scary: " << endl;
  getline( cin, weirdGroup );

  string somethingGross;
  cout << "Name a food item you detested as a child: " << endl;
  getline( cin, somethingGross );

  // Finally, let's print out the headlines!
  cout << endl << endl << endl
       << "And now, today's headlines from the C++ News Wire:" << endl;
  cout << "--------------------------------------------------" << endl;

  cout << "ALIENS SHAPED LIKE " << color << " " << amorphousObject
       << " INVADE THE EARTH, KIDNAP " << celebrityActor << ", "
       << "RESURRECT " << deadGuy << "!" << endl << endl;

  cout << userName
       << " RELEASES NEW ALBUM! " << smallNumber
       << " COPIES EXPECTED TO BE SOLD!"
       << endl << endl;

  cout << politician << " CAUGHT IN LOVE TRIANGLE WITH "
       << cartoonCharacter << " AND SECRET "
       << weirdGroup << " CULT LEADER!" << endl << endl;

  cout << "WORLD'S LARGEST BABY BORN - WEIGHS " << largeNumber
       << " POUNDS, EATS " << smallNumber << " TONS OF "
       << somethingGross << " EVERY DAY!" << endl << endl;

  // And we're done!
  return 0;
}
</code></pre>
<p>Innocent enough to be sure...</p>
<p>Upon executing this little piece of code, the request for input on line 33 would "skip" without receiving anything from the console, moving ahead to the request for input on line 37. I tried playing with the different uses of "cin" along with "getline" to reproduce / remove this "input skipping". I commented out blocks of the code to try to isolate any extra return characters from somehow ending up in the input stream buffer (denoted by cin) until at last I discovered a workaround.</p>
<h2>the workaround</h2>
<p>If you'll notice in the source code, I'm switching between several different data types for grabbing input from the console. The code is requesting string / char data, ints, floats and more strings. Even going through C++ manuals and references couldn't account for the "input skip".</p>
<p><strong>It was only when I tried grouping the input data types together, that the code started working as intended.</strong></p>
<pre><code>
/*
 * Project4_1.cpp
 *
 *  Created on: Jan 7, 2010
 *      Author: Administrator
 */

#include <iostream>
#include <string>

using namespace std;

int main()
{
  // First, let's welcome the user
  cout << "Welcome to the C++ News Network!" << endl << endl;

  // Then, let's input several values to plug into our headlines...
  // Note that the questions don't always match the names of the
  // variables because we are trying to surprise the player.

  char userName[80];
  char color[80];
  char amorphousObject[80];
  int smallNumber = 0;
  double largeNumber = 0.0;

  cout << "Please type in your first name: ";
  cin.getline(userName,80);

  cout << "Tell us your least favourite color: ";
  cin.getline(color, 80 );

  cout << "Which vegetables have the weirdest shapes?: ";
  cin.getline( amorphousObject, 80 );

  string deadGuy;
  cout << "Name a famous dead person:" << endl;
  getline( cin, deadGuy );

  string celebrityActor;
  cout << "Who is your favorite actor?" << endl;
  getline( cin, celebrityActor );

  string politician;
  cout << "Name a current world leader:" << endl;
  getline( cin, politician );

  string cartoonCharacter;
  cout << "Who is your favourite cartoon character?" << endl;
  getline( cin, cartoonCharacter );

  string weirdGroup;
  cout << "Name a hobby or a profession you find scary: " << endl;
  getline( cin, weirdGroup );

  string somethingGross;
  cout << "Name a food item you detested as a child: " << endl;
  getline( cin, somethingGross );

  cout << "How many siblings do you have?: ";
  cin >> smallNumber;

  cout << "How much money would you like to earn every year?: ";
  cin >> largeNumber;

  // Finally, let's print out the headlines!
  cout << endl << endl << endl
       << "And now, today's headlines from the C++ News Wire:" << endl;
  cout << "--------------------------------------------------" << endl;

  cout << "ALIENS SHAPED LIKE " << color << " " << amorphousObject
       << " INVADE THE EARTH, KIDNAP " << celebrityActor << ", "
       << "RESURRECT " << deadGuy << "!" << endl << endl;

  cout << userName
       << " RELEASES NEW ALBUM! " << smallNumber
       << " COPIES EXPECTED TO BE SOLD!"
       << endl << endl;

  cout << politician << " CAUGHT IN LOVE TRIANGLE WITH "
       << cartoonCharacter << " AND SECRET "
       << weirdGroup << " CULT LEADER!" << endl << endl;

  cout << "WORLD'S LARGEST BABY BORN - EATS " << smallNumber << " TONS OF "
       << somethingGross << " EVERY DAY!" << endl << endl;

  // And we're done!

  return 0;
}
</pre>
<p></code></p>
<p>hope that helps!</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-wealth">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://www.wazooinc.com/discovering-a-bug-with-the-cin-and-cout-streams.html/feed" rel="" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://www.wazooinc.com/discovering-a-bug-with-the-cin-and-cout-streams.html&amp;title=discovering+a+bug+with+the+cin+and+cout+streams" rel="" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://www.wazooinc.com/discovering-a-bug-with-the-cin-and-cout-streams.html&amp;title=discovering+a+bug+with+the+cin+and+cout+streams" rel="" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.diigo.com/post?url=http://www.wazooinc.com/discovering-a-bug-with-the-cin-and-cout-streams.html&amp;title=discovering+a+bug+with+the+cin+and+cout+streams&amp;desc=A%20reader%20had%20sent%20in%20the%20following%20request%20for%20help%2C%20from%20my%20section%20in%20the%20material%20which%20deals%20with%20cin%2C%20cout%20and%20basic%20serialization%20using%20C%2B%2B%3A%0D%0A%0D%0Ausing%20bloodshed%20and%20your%20code%2C%20Listing%204.1%20%28pg%2046%20-%2048%29in%20Learning%20C%2B%2B%20by%0D%0AMaking%20Games%20compiles%20well%20but%20skips%20the%20input%20stage%20for%20amorphouseObject.%0D" rel="" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://www.wazooinc.com/discovering-a-bug-with-the-cin-and-cout-streams.html&amp;t=discovering+a+bug+with+the+cin+and+cout+streams" rel="" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://www.wazooinc.com/discovering-a-bug-with-the-cin-and-cout-streams.html&amp;imageurl=" rel="" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.mister-wong.com/addurl/?bm_url=http://www.wazooinc.com/discovering-a-bug-with-the-cin-and-cout-streams.html&amp;bm_description=discovering+a+bug+with+the+cin+and+cout+streams&amp;plugin=sexybookmarks" rel="" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.mixx.com/submit?page_url=http://www.wazooinc.com/discovering-a-bug-with-the-cin-and-cout-streams.html&amp;title=discovering+a+bug+with+the+cin+and+cout+streams" rel="" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://www.wazooinc.com/discovering-a-bug-with-the-cin-and-cout-streams.html&amp;title=discovering+a+bug+with+the+cin+and+cout+streams" rel="" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://www.wazooinc.com/discovering-a-bug-with-the-cin-and-cout-streams.html&amp;title=discovering+a+bug+with+the+cin+and+cout+streams" rel="" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://technorati.com/faves?add=http://www.wazooinc.com/discovering-a-bug-with-the-cin-and-cout-streams.html" rel="" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=discovering+a+bug+with+the+cin+and+cout+streams+-+http://b2l.me/agz32w&amp;source=shareaholic" rel="" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://www.wazooinc.com/discovering-a-bug-with-the-cin-and-cout-streams.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
