<?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>killianfaughnan.com &#187; c++</title>
	<atom:link href="http://killianfaughnan.com/category/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://killianfaughnan.com</link>
	<description>Just another WordPress site</description>
	<lastBuildDate>Wed, 23 Feb 2011 09:05:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Basic C++ Calculator</title>
		<link>http://killianfaughnan.com/2008/11/20/basic-cpp-calculator/</link>
		<comments>http://killianfaughnan.com/2008/11/20/basic-cpp-calculator/#comments</comments>
		<pubDate>Thu, 20 Nov 2008 15:56:21 +0000</pubDate>
		<dc:creator>kfaughnan</dc:creator>
				<category><![CDATA[c++]]></category>
		<category><![CDATA[calculator]]></category>

		<guid isPermaLink="false">http://killianfaughnan.com/?p=100</guid>
		<description><![CDATA[The below is a simple program to add, subtract, multiply and divide integer numbers. It&#8217;s the first program I wrote in C++ as a tester and most of the basic concepts are covered so I&#8217;ll go through them here in case they&#8217;re of help to someone else! I&#8217;ll admit the explainations below are a bit [...]]]></description>
			<content:encoded><![CDATA[<p>The below is a simple program to add, subtract, multiply and divide integer numbers. It&#8217;s the first program I wrote in C++ as a tester and most of the basic concepts are covered so I&#8217;ll go through them here in case they&#8217;re of help to someone else! I&#8217;ll admit the explainations below are a bit patchy, but this is more aimed at someone with previous programming experience who is looking to try out C++.</p>
<p>To start off we have to include our basic input and output bit&#8217;s and pieces and to declare we&#8217;re using the standard namespace. To do so stick in the following in the first two lines of your program. We won&#8217;t worry about these too much for now.</p>
<pre>#include &lt;iostream&gt;
using namespace std;</pre>
<p>Now we&#8217;ll get into some basic subroutine (or function) for adding and subtracting etc. We&#8217;ll start with an addition function.</p>
<pre>int sum(int x,int y)
{
    return x+y;
}</pre>
<p>So first we declare the subroutine as an int (as our calculator is only going to deal with int&#8217;s). So we begin with an <em>int sum</em>. This is telling the compiler that the return value from this subroutine will be an integer value. It is also naming the subroutine as <em>sum </em>which is what we will use to call it later. We then tell the compiler that this subroutine will take in two parameters, both ints and call them <em>x</em> and <em>y </em>respectively.</p>
<p>Once the function has been called with the correct number of arguments it will add the values together and return the result. That&#8217;s it for addition. Subtraction and multiplication are exactly the same except for the obvious difference of using a &#8211; or * instead of a +. As below.</p>
<pre>int difference(int x,int y)
{
    return x - y;
}
int product(int x,int y)
{
    return x * y;
}</pre>
<p>Division is a little different. This is because if someone running the program tries to divide by zero the program will throw a <strong>big dirty error</strong>. The way to get around this is to check if the unputted value is a zero is quite simple. So we start off with the same construct as before, but mix up the inner workings a little.</p>
<pre>int quotient(int x,int y)
{
    if( x != 0 &amp;&amp; y != 0 )
    {
        return x / y;
    }
    else{
        cout &lt;&lt; "nCannot divide by zero!n" &lt;&lt; endl;
        return false;
    }
}</pre>
<p>In the above we simply take in the <em>x</em> and <em>y</em> as normal, but this time instead of simply dividing them, we check their value first using an if statement. So inside the subrouting we will start off with:</p>
<pre>int quotient(int x,int y)
{
    if( x != 0 &amp;&amp; y != 0 )
    {
        return x / y;
    }
}</pre>
<p>This means that we are saying that if <em>x</em> is not equal (<em>!=</em>) to zero, and (<em>&amp;&amp;</em>) if <em>y</em> is not equal (<em>!=</em>) to zero then go ahead and divide them. That&#8217;s all well and good, but what if one of our values is a zero? Do we just ignore it? No, we have to inform the user of the error. SO now we add in an else statement.</p>
<pre>int quotient(int x,int y)
{
    if( x != 0 &amp;&amp; y != 0 )
    {
        return x / y;
    }
    else{
        cout &lt;&lt; "nCannot divide by zero!n" &lt;&lt; endl;
        return false;
    }
}</pre>
<p>Our <em>else</em> statement will now output the text <em>&#8220;Cannot divide by zero!&#8221; </em>if<em> </em>we find that one of the inputted integers is a zero, and return a value of false. That&#8217;s the main functionality of a calculator covered there now. The following two subroutines are just for printing out a semi-acceptable menu and giving the user the option of the different functions and exiting when required.</p>
<p>This is the menu function:</p>
<pre>void menu(){
    cout &lt;&lt; "** Menu **" &lt;&lt; endl;
    for(int i=0;i&lt;20;i++){
        cout &lt;&lt; "-";
    }
    cout &lt;&lt; "n1. Sum" &lt;&lt; endl;
    cout &lt;&lt; "2. Difference" &lt;&lt; endl;
    cout &lt;&lt; "3. Product" &lt;&lt; endl;
    cout &lt;&lt; "4. Quotient" &lt;&lt; endl;
    cout &lt;&lt; "5. Exit" &lt;&lt; endl;
}</pre>
<p>This is the prompt for accepting input from the user:</p>
<pre>void prompt(int* p_x,int* p_y){
    cout &lt;&lt; "nEnter first integer: ";
    cin &gt;&gt; *p_x;
    cout &lt;&lt; "Enter second integer: ";
    cin &gt;&gt; *p_y;
}</pre>
<p>This is the main function which is called by the program when run. In it we declare our variables and get ready to accept whatever input the user enters.</p>
<pre>int main()
{
    int x;
    int y;
    int option;
    int answer;

    while(5!=option)
    {
        menu();
        cout &lt;&lt; "Please enter choice: ";
        cin &gt;&gt; option;
        if(1==option)
        {
            prompt(&amp;x,&amp;y);
            answer = sum(x,y);
        }
        else if(2==option)
        {
            prompt(&amp;x,&amp;y);
            answer = difference(x,y);
        }
        else if(3==option)
        {
            prompt(&amp;x,&amp;y);
            answer = product(x,y);
        }
        else if(4==option)
        {
            prompt(&amp;x,&amp;y);
            answer = quotient(x,y);
        }
        if(5!=option &amp;&amp; answer != false){
            cout &lt;&lt; "nAnswer is: " &lt;&lt; answer &lt;&lt; "n" &lt;&lt; endl;
        }
    }

    return 0;
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://killianfaughnan.com/2008/11/20/basic-cpp-calculator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

