msgbartop
Just another WordPress site
msgbarbottom

20 Nov 08 Basic C++ Calculator

The below is a simple program to add, subtract, multiply and divide integer numbers. It’s the first program I wrote in C++ as a tester and most of the basic concepts are covered so I’ll go through them here in case they’re of help to someone else! I’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++.

To start off we have to include our basic input and output bit’s and pieces and to declare we’re using the standard namespace. To do so stick in the following in the first two lines of your program. We won’t worry about these too much for now.

#include <iostream>
using namespace std;

Now we’ll get into some basic subroutine (or function) for adding and subtracting etc. We’ll start with an addition function.

int sum(int x,int y)
{
    return x+y;
}

So first we declare the subroutine as an int (as our calculator is only going to deal with int’s). So we begin with an int sum. This is telling the compiler that the return value from this subroutine will be an integer value. It is also naming the subroutine as sum 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 x and y respectively.

Once the function has been called with the correct number of arguments it will add the values together and return the result. That’s it for addition. Subtraction and multiplication are exactly the same except for the obvious difference of using a – or * instead of a +. As below.

int difference(int x,int y)
{
    return x - y;
}
int product(int x,int y)
{
    return x * y;
}

Division is a little different. This is because if someone running the program tries to divide by zero the program will throw a big dirty error. 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.

int quotient(int x,int y)
{
    if( x != 0 && y != 0 )
    {
        return x / y;
    }
    else{
        cout << "nCannot divide by zero!n" << endl;
        return false;
    }
}

In the above we simply take in the x and y 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:

int quotient(int x,int y)
{
    if( x != 0 && y != 0 )
    {
        return x / y;
    }
}

This means that we are saying that if x is not equal (!=) to zero, and (&&) if y is not equal (!=) to zero then go ahead and divide them. That’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.

int quotient(int x,int y)
{
    if( x != 0 && y != 0 )
    {
        return x / y;
    }
    else{
        cout << "nCannot divide by zero!n" << endl;
        return false;
    }
}

Our else statement will now output the text “Cannot divide by zero!” if we find that one of the inputted integers is a zero, and return a value of false. That’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.

This is the menu function:

void menu(){
    cout << "** Menu **" << endl;
    for(int i=0;i<20;i++){
        cout << "-";
    }
    cout << "n1. Sum" << endl;
    cout << "2. Difference" << endl;
    cout << "3. Product" << endl;
    cout << "4. Quotient" << endl;
    cout << "5. Exit" << endl;
}

This is the prompt for accepting input from the user:

void prompt(int* p_x,int* p_y){
    cout << "nEnter first integer: ";
    cin >> *p_x;
    cout << "Enter second integer: ";
    cin >> *p_y;
}

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.

int main()
{
    int x;
    int y;
    int option;
    int answer;

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

    return 0;
}