<?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"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: 5.3 &#8212; Switch statements</title>
	<atom:link href="http://www.learncpp.com/cpp-tutorial/53-switch-statements/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.learncpp.com/cpp-tutorial/53-switch-statements/</link>
	<description></description>
	<lastBuildDate>Tue, 07 Feb 2012 12:30:29 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: C++_4_fun</title>
		<link>http://www.learncpp.com/cpp-tutorial/53-switch-statements/comment-page-1/#comment-96845</link>
		<dc:creator>C++_4_fun</dc:creator>
		<pubDate>Sun, 15 Jan 2012 22:12:46 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/53-switch/#comment-96845</guid>
		<description>Sorry, I&#039;m not sure why some of the code is still missing. Any suggestions?</description>
		<content:encoded><![CDATA[<p>Sorry, I&#8217;m not sure why some of the code is still missing. Any suggestions?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: C++_4_fun</title>
		<link>http://www.learncpp.com/cpp-tutorial/53-switch-statements/comment-page-1/#comment-96844</link>
		<dc:creator>C++_4_fun</dc:creator>
		<pubDate>Sun, 15 Jan 2012 22:11:18 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/53-switch/#comment-96844</guid>
		<description>?? Somo of my code was missing...
The main section should have read
&lt;pre&gt; 
int _tmain(int argc, _TCHAR* argv[])
{
	using namespace std;
	cout &lt;&gt; x;
	cout &lt;&lt; endl &lt;&gt; y;
	cout &lt;&lt; endl &lt;&gt; chOperator;
	Calculate(x, y, chOperator);
	cout &lt;&lt; &quot;The answer is: &quot; &lt;&lt; endl &lt;&lt; Calculate(x, y, chOperator);
	
	return 0;
}
&lt;pre&gt;</description>
		<content:encoded><![CDATA[<p>?? Somo of my code was missing&#8230;<br />
The main section should have read</p>
<pre>
int _tmain(int argc, _TCHAR* argv[])
{
	using namespace std;
	cout &lt;&gt; x;
	cout &lt;&lt; endl &lt;&gt; y;
	cout &lt;&lt; endl &lt;&gt; chOperator;
	Calculate(x, y, chOperator);
	cout &lt;&lt; &quot;The answer is: &quot; &lt;&lt; endl &lt;&lt; Calculate(x, y, chOperator);

	return 0;
}
</pre>
<pre></pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: C++_4_fun</title>
		<link>http://www.learncpp.com/cpp-tutorial/53-switch-statements/comment-page-1/#comment-96843</link>
		<dc:creator>C++_4_fun</dc:creator>
		<pubDate>Sun, 15 Jan 2012 22:08:12 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/53-switch/#comment-96843</guid>
		<description>Hi,
I tried to write a program using the function in the quiz to do basic calculation with no success. I&#039;m not sure what I&#039;m doing wrong but when I try to print the value returned to Calculate(x, y, chOperator) by the arguments in the switch I get gibberish. The code is below. Any advice is appreciated.

&lt;pre&gt;
#include &quot;stdafx.h&quot;
#include
using namespace std;

int Calculate(int x, int y, char chOperator)
{
	switch (chOperator)
	{
	case &#039;+&#039;:
			return x + y;
			break;
	case &#039;-&#039;:
			return x - y;
			break;
		case &#039;*&#039;:
			return x * y;
			break;
		case &#039;/&#039;:
			return x/y;
			break;
	}
}

int _tmain(int argc, _TCHAR* argv[])
{
	using namespace std;
	cout &lt;&gt; x;
	cout &lt;&lt; endl &lt;&gt; y;
	cout &lt;&lt; endl &lt;&gt; chOperator;
	Calculate(x, y, chOperator);
	cout &lt;&lt; &quot;The answer is: &quot; &lt;&lt; endl &lt;&lt; Calculate(x, y, chOperator);
	
	return 0;
}
&lt;pre&gt;</description>
		<content:encoded><![CDATA[<p>Hi,<br />
I tried to write a program using the function in the quiz to do basic calculation with no success. I&#8217;m not sure what I&#8217;m doing wrong but when I try to print the value returned to Calculate(x, y, chOperator) by the arguments in the switch I get gibberish. The code is below. Any advice is appreciated.</p>
<pre>
#include "stdafx.h"
#include
using namespace std;

int Calculate(int x, int y, char chOperator)
{
	switch (chOperator)
	{
	case '+':
			return x + y;
			break;
	case '-':
			return x - y;
			break;
		case '*':
			return x * y;
			break;
		case '/':
			return x/y;
			break;
	}
}

int _tmain(int argc, _TCHAR* argv[])
{
	using namespace std;
	cout &lt;&gt; x;
	cout &lt;&lt; endl &lt;&gt; y;
	cout &lt;&lt; endl &lt;&gt; chOperator;
	Calculate(x, y, chOperator);
	cout &lt;&lt; &quot;The answer is: &quot; &lt;&lt; endl &lt;&lt; Calculate(x, y, chOperator);

	return 0;
}
</pre>
<pre></pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: zingmars</title>
		<link>http://www.learncpp.com/cpp-tutorial/53-switch-statements/comment-page-1/#comment-95753</link>
		<dc:creator>zingmars</dc:creator>
		<pubDate>Tue, 19 Jul 2011 11:19:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/53-switch/#comment-95753</guid>
		<description>The value would be correct, though everything after the comma would be removed.</description>
		<content:encoded><![CDATA[<p>The value would be correct, though everything after the comma would be removed.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SelfExplanatory</title>
		<link>http://www.learncpp.com/cpp-tutorial/53-switch-statements/comment-page-1/#comment-95699</link>
		<dc:creator>SelfExplanatory</dc:creator>
		<pubDate>Tue, 12 Jul 2011 17:15:25 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/53-switch/#comment-95699</guid>
		<description>In your solution for the quiz, wouldn&#039;t (case &#039;/&#039;: return nX / nY) use integer division and return an incorrect value?</description>
		<content:encoded><![CDATA[<p>In your solution for the quiz, wouldn&#8217;t (case &#8216;/&#8217;: return nX / nY) use integer division and return an incorrect value?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Groovychicken</title>
		<link>http://www.learncpp.com/cpp-tutorial/53-switch-statements/comment-page-1/#comment-95272</link>
		<dc:creator>Groovychicken</dc:creator>
		<pubDate>Wed, 23 Mar 2011 04:29:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/53-switch/#comment-95272</guid>
		<description>&lt;pre&gt;#include &quot;stdafx.h&quot;
#include 
#include 

void CalculateEquation(int nX, char chX, int nY)
{	
	using namespace std;

	switch(chX)
	{
		case(&#039;+&#039;):
			cout &lt;&lt; &quot;The result of: &quot; &lt;&lt; nX &lt;&lt; &quot; &quot; &lt;&lt; chX &lt;&lt; &quot; &quot; &lt;&lt; nY &lt;&lt; &quot; is &quot; &lt;&lt; nX + nY &lt;&lt; endl;
			break;
		case(&#039;-&#039;):
			cout &lt;&lt; &quot;The result of: &quot; &lt;&lt; nX &lt;&lt; &quot; &quot; &lt;&lt; chX &lt;&lt; &quot; &quot; &lt;&lt; nY &lt;&lt; &quot; is &quot; &lt;&lt; nX - nY &lt;&lt; endl;
			break;
		case(&#039;*&#039;):
			cout &lt;&lt; &quot;The result of: &quot; &lt;&lt; nX &lt;&lt; &quot; &quot; &lt;&lt; chX &lt;&lt; &quot; &quot; &lt;&lt; nY &lt;&lt; &quot; is &quot; &lt;&lt; nX * nY &lt;&lt; endl;
			break;
		case(&#039;/&#039;):
			cout &lt;&lt; &quot;The result of: &quot; &lt;&lt; nX &lt;&lt; &quot; &quot; &lt;&lt; chX &lt;&lt; &quot; &quot; &lt;&lt; nY &lt;&lt; &quot; is &quot; &lt;&lt; nX / nY &lt;&lt; endl;
			break;
		default:
			cout &lt;&lt; &quot;ERROR!&quot; &lt;&lt; endl;
			exit(0);
	}
}
	
int main()
{
	using namespace std;

	cout &lt;&gt; nX;
	cout &lt;&lt; &quot;You have entered: &quot; &lt;&lt; nX &lt;&lt; &quot; as your first integer. &quot; &lt;&lt; endl;

	cout &lt;&gt; chX;
	cout &lt;&lt; &quot;You have entered: &quot; &lt;&lt; chX &lt;&lt; &quot; as your mathematical operator. &quot; &lt;&lt; endl;

	cout &lt;&gt; nY;
	cout &lt;&lt; &quot;You have entered: &quot; &lt;&lt; nY &lt;&lt; &quot; as your second integer. &quot; &lt;&lt; endl;

	CalculateEquation(nX,chX,nY);

	return 0;
}
&lt;pre&gt;</description>
		<content:encoded><![CDATA[<pre>#include "stdafx.h"
#include
#include 

void CalculateEquation(int nX, char chX, int nY)
{
	using namespace std;

	switch(chX)
	{
		case('+'):
			cout &lt;&lt; &quot;The result of: &quot; &lt;&lt; nX &lt;&lt; &quot; &quot; &lt;&lt; chX &lt;&lt; &quot; &quot; &lt;&lt; nY &lt;&lt; &quot; is &quot; &lt;&lt; nX + nY &lt;&lt; endl;
			break;
		case(&#039;-&#039;):
			cout &lt;&lt; &quot;The result of: &quot; &lt;&lt; nX &lt;&lt; &quot; &quot; &lt;&lt; chX &lt;&lt; &quot; &quot; &lt;&lt; nY &lt;&lt; &quot; is &quot; &lt;&lt; nX - nY &lt;&lt; endl;
			break;
		case(&#039;*&#039;):
			cout &lt;&lt; &quot;The result of: &quot; &lt;&lt; nX &lt;&lt; &quot; &quot; &lt;&lt; chX &lt;&lt; &quot; &quot; &lt;&lt; nY &lt;&lt; &quot; is &quot; &lt;&lt; nX * nY &lt;&lt; endl;
			break;
		case(&#039;/&#039;):
			cout &lt;&lt; &quot;The result of: &quot; &lt;&lt; nX &lt;&lt; &quot; &quot; &lt;&lt; chX &lt;&lt; &quot; &quot; &lt;&lt; nY &lt;&lt; &quot; is &quot; &lt;&lt; nX / nY &lt;&lt; endl;
			break;
		default:
			cout &lt;&lt; &quot;ERROR!&quot; &lt;&lt; endl;
			exit(0);
	}
}

int main()
{
	using namespace std;

	cout &lt;&gt; nX;
	cout &lt;&lt; &quot;You have entered: &quot; &lt;&lt; nX &lt;&lt; &quot; as your first integer. &quot; &lt;&lt; endl;

	cout &lt;&gt; chX;
	cout &lt;&lt; &quot;You have entered: &quot; &lt;&lt; chX &lt;&lt; &quot; as your mathematical operator. &quot; &lt;&lt; endl;

	cout &lt;&gt; nY;
	cout &lt;&lt; &quot;You have entered: &quot; &lt;&lt; nY &lt;&lt; &quot; as your second integer. &quot; &lt;&lt; endl;

	CalculateEquation(nX,chX,nY);

	return 0;
}
</pre>
<pre></pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: buffpony</title>
		<link>http://www.learncpp.com/cpp-tutorial/53-switch-statements/comment-page-1/#comment-95020</link>
		<dc:creator>buffpony</dc:creator>
		<pubDate>Thu, 09 Dec 2010 23:32:59 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/53-switch/#comment-95020</guid>
		<description>Actually forget that.... i fixed it. it was stupid. now i get the error when the program runs. it lets me enter numbers and operations, but crashed when performing them.</description>
		<content:encoded><![CDATA[<p>Actually forget that&#8230;. i fixed it. it was stupid. now i get the error when the program runs. it lets me enter numbers and operations, but crashed when performing them.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: buffpony</title>
		<link>http://www.learncpp.com/cpp-tutorial/53-switch-statements/comment-page-1/#comment-95019</link>
		<dc:creator>buffpony</dc:creator>
		<pubDate>Thu, 09 Dec 2010 23:28:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/53-switch/#comment-95019</guid>
		<description>this code gives me the error: &quot;Calculate cannot take one argument. What should i do?
#include 
using namespace std;
int num1, num2;
char opp;
int Calculate(int num1,int num2, char opp)
{
	switch(opp)
	{
	case &#039;+&#039; &#124;&#124; &#039;plus&#039;:
		return num1+num2;				
	case &#039;-&#039;:
		return num1-num2;
	case &#039;*&#039;:
		return num1*num2;
	case &#039;/&#039;:
		cout &lt;&lt; &quot;The first number is didided by the second&quot;;
		return num1/num2;
	default:
		cout &lt;&lt; &quot;Error-program will exit&quot;;
		exit(1);
		}
}
int main()
{ 
	cout &lt;&gt; num1;
	cin &gt;&gt; num2;
	cout &lt;&lt; &quot;Please Enter an opperation symbol: &quot; &lt;&gt; opp;
	Calculate(opp);
}</description>
		<content:encoded><![CDATA[<p>this code gives me the error: &#8220;Calculate cannot take one argument. What should i do?<br />
#include<br />
using namespace std;<br />
int num1, num2;<br />
char opp;<br />
int Calculate(int num1,int num2, char opp)<br />
{<br />
	switch(opp)<br />
	{<br />
	case &#8216;+&#8217; || &#8216;plus&#8217;:<br />
		return num1+num2;<br />
	case &#8216;-&#8217;:<br />
		return num1-num2;<br />
	case &#8216;*&#8217;:<br />
		return num1*num2;<br />
	case &#8216;/&#8217;:<br />
		cout &lt;&lt; &quot;The first number is didided by the second&quot;;<br />
		return num1/num2;<br />
	default:<br />
		cout &lt;&lt; &quot;Error-program will exit&quot;;<br />
		exit(1);<br />
		}<br />
}<br />
int main()<br />
{<br />
	cout &lt;&gt; num1;<br />
	cin &gt;&gt; num2;<br />
	cout &lt;&lt; &quot;Please Enter an opperation symbol: &quot; &lt;&gt; opp;<br />
	Calculate(opp);<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jesper</title>
		<link>http://www.learncpp.com/cpp-tutorial/53-switch-statements/comment-page-1/#comment-93100</link>
		<dc:creator>Jesper</dc:creator>
		<pubDate>Sat, 02 Oct 2010 10:11:17 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/53-switch/#comment-93100</guid>
		<description>Hi Alex

I want to say that i love these tutorials you&#039;ve made. I&#039;ve been away from
c++ for some years and I needed a brush-up and this is just perfect. Great job!!
I have something to add to this section on the switch-case construct though.
The real reason why there is two ways to achieve the same thing i.e. if-elseif-else and switch-case
is because of execution-speed. if-elseif-else will always execute in linear time while switch-case
is implemented using a jump-table meaning it will execute in constant time. Though the jump-table
will only be used if the case-values are not spread out too far, because that would make the
jump-table too big.
So if it is possible you should always use switch-case and keep switch-values in the low range.</description>
		<content:encoded><![CDATA[<p>Hi Alex</p>
<p>I want to say that i love these tutorials you&#8217;ve made. I&#8217;ve been away from<br />
c++ for some years and I needed a brush-up and this is just perfect. Great job!!<br />
I have something to add to this section on the switch-case construct though.<br />
The real reason why there is two ways to achieve the same thing i.e. if-elseif-else and switch-case<br />
is because of execution-speed. if-elseif-else will always execute in linear time while switch-case<br />
is implemented using a jump-table meaning it will execute in constant time. Though the jump-table<br />
will only be used if the case-values are not spread out too far, because that would make the<br />
jump-table too big.<br />
So if it is possible you should always use switch-case and keep switch-values in the low range.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Compiler</title>
		<link>http://www.learncpp.com/cpp-tutorial/53-switch-statements/comment-page-1/#comment-91402</link>
		<dc:creator>Compiler</dc:creator>
		<pubDate>Sat, 04 Sep 2010 20:39:17 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/53-switch/#comment-91402</guid>
		<description>I tried to call the function this way:
int main()
{
Calculate(); //Didnt work, calculation doesnt take zero arguments
Calculate(int x,int y, char Operand); //didnt work either
return 0;
}</description>
		<content:encoded><![CDATA[<p>I tried to call the function this way:<br />
int main()<br />
{<br />
Calculate(); //Didnt work, calculation doesnt take zero arguments<br />
Calculate(int x,int y, char Operand); //didnt work either<br />
return 0;<br />
}</p>
]]></content:encoded>
	</item>
</channel>
</rss>

