Here is the code for simple calculator implemented using switch.

#include <iostream>

using namespace std;

int main()
{
    int num1, num2; //input two numbers for operation like +,-,/,*
    int sum = 0;
    char op;    //variable for operator

    cout<<"Enter num 1: "<<flush;
    cin >> num1;
    cout<<"Enter num 2: "<<flush;
    cin >> num2;

    do{

        cout<<"\nPlease select the operator (+,-,*,/) : "<<flush;
        cin >> op;

        switch(op)
        {
            case 43:
            {
                sum = num1 + num2;
                cout<<num1 <<" + " <<num2 <<" = " <<sum <<endl;
            }
            break;

            case 45:
            {
                sum = num1 - num2;
                cout<<num1 <<" - " <<num2 <<" = " <<sum <<endl;
            }
            break;

            case 42:
            {
                sum = num1 * num2;
                cout<<num1 <<" * " <<num2 <<" = " <<sum <<endl;
            }
            break;

            case 47:
            {
                if(num2 == 0)
                {
                    cout<<"\nDenominator can't be zero !"<<endl;
                }
                else
                {
                    sum = num1 / num2;
                    cout<<num1 <<" / " <<num2 <<" = " <<sum <<endl;
                }
            }
            break;

            default:
                cout<<"\nInvalid operator !"<<endl;

        }   //end of switch

    }while(op != '+' && op != '-' && op != '*' && op != '/');

    return 0;
}

Hope this helps !

thanks brother but one problem why do you had to use switch cases values as 42,45,43..I mean why not use 1,2,3,4?

because 43, 45, 42, 47 are the char code of +, -, *, / respectively. If the user inputs + operator case 43 will be executed.

If user inputs - operator case 45 will be executed and so on ... Please vote up if it helps.

Following code will help you to implement basic calculator using if ese .. if else conditions.

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int num1, num2, result;
    string op;

    cout<<"Enter value for num1 : "<<endl;
    cin>>num1;
    cout<<"Enter value for num2 : "<<endl;
    cin>>num2;

    cout<<"Please select the operator(+,-,*,/)"<<endl;
    cin>>op;

    if(op=="+")
    {
        result=num1+num2;
        cout<<num1 <<" + " <<num2 <<" = " <<result <<endl;
    }
    else if(op=="-")
    {
        result=num1-num2;
        cout<<num1 <<" - " <<num2 <<" = " <<result <<endl;
    }
    else if(op=="*")
    {
        result=num1*num2;
        cout<<num1 <<" * " <<num2 <<" = " <<result <<endl;
    }
    else if(op=="/")
    {
        result=num1/num2;
        cout<<num1 <<" / " <<num2 <<" = " <<result <<endl;
    }
    else
    {
        cout<<"Invalid operator !"<<endl;
    }

    return 0;
}

thanks a lot...now I'm getting the concepts...I've another question as I'm a begginer and don't get this concept as yet..what is the need of do..while loop here?I mean what is its work in this program?thanks

As you can see I have wrap the do while loop in the operator input that is op(+,-,*,/) and the switch.

do
{
    cout<<"\nPlease select the operator (+,-,*,/) : "<<flush;
    cin >> op;

    switch(op)
    {
        case 43:
        {
            sum = num1 + num2;
            cout<<num1 <<" + " <<num2 <<" = " <<sum <<endl;
        }
        break;

        case 45:
        {
            sum = num1 - num2;
            cout<<num1 <<" - " <<num2 <<" = " <<sum <<endl;
        }
        break;

        case 42:
        {
            sum = num1 * num2;
            cout<<num1 <<" * " <<num2 <<" = " <<sum <<endl;
        }
        break;

        case 47:
        {
            if(num2 == 0)
            {
                cout<<"\nDenominator can't be zero !"<<endl;
            }
            else
            {
                sum = num1 / num2;
                cout<<num1 <<" / " <<num2 <<" = " <<sum <<endl;
            }
        }
        break;

        default:
            cout<<"\nInvalid operator !"<<endl;

    } //end of switch

}while(op != '+' && op != '-' && op != '*' && op != '/');

Do while loop will run at least one time(or first time) the code in its body and check the condition in the end if it is true or not.

do while loop works in this manner do {what ever is in the body} while(condition) is true.

If the condition is true which means user did not input + and - and * and /, the loop will run again and ask the user to input operator again.

In other case if the condition is false the loop will terminate. If you have any query regarding your program please feel free to ask.

you can use of character in case value .
look here:-

        case '+':
        {
        sum = num1 + num2;
        cout<<num1 <<" + " <<num2 <<" = " <<sum <<endl;
        }
        break;
        case '-':
        {
        sum = num1 - num2;
        cout<<num1 <<" - " <<num2 <<" = " <<sum <<endl;
        }
        break;
        case '*':
        {
        sum = num1 * num2;
        cout<<num1 <<" * " <<num2 <<" = " <<sum <<endl;
        }
        break;
        case '/':
        {
        if(num2 == 0)
        {
            cout<<"\nDenominator can't be zero !"<<endl;
        }
        else
        {
            sum = num1 / num2;
            cout<<num1 <<" / " <<num2 <<" = " <<sum <<endl;
        }
        }

Edited 3 Years Ago by rishif2

This article has been dead for over six months. Start a new discussion instead.