Friday, December 28, 2012

Building a very simple calculator program in C++ console using switch statements

Assume that I want the user to enter two numbers and an operator out of + - * and / to perform a certain calculation  in a c++ console program.

As a control structure, you may tend to use if-else blocks but you can do the same task more efficiently if you used switch statements.

you also need to check for invalid inputs and let the user to use your calculator to do any number of times until he explicitly requests to quit the program. 

To let the use do any number of calculations, you have to think of some way to iterate your task until the user quits. A loop definitely. Let's use a while loop since it's simple. We can't use a for loop this time because you don't know how many calculations your user may do.

Observe the following source code and follow the comments for clarification.

#include<iostream>
using namespace std;

int main()
{
char operator_symbol,decision;  //operator_symbol to hold the calculation symbol
                                                 //decision will hold the user's decision to continue the loop 
float num1,num2,answer=0; //num1 and num2 holds the user entered numbers 
decision='y';                         //initially, the decision is assigned as yes

       while(decision=='y')
{
        cout<<"Enter first number , Operator, second number :"; //display the order that user should 
                                                                                        //input the numbers and the operator
        cin>>num1>>operator_symbol>>num2;              //get them for variables

switch(operator_symbol)                                //switch compares this variable
{
case '+':answer=num1+num2;                        //if the operator is + , then add
break;                                                             //go to the end of switch block

case '-':answer=num1-num2;                          //if the operator is - , then substract
break;                                                             //go to the end of switch block
case '*':answer=num1*num2;                         //if the operator is * , then multiply
break;                                                              //go to the end of switch block

case '/':answer=num1/num2;                           //if the operator is / , then divide
break;                                                              //go to the end of switch block

default:cout<<"Wrong operator! Please retry"<<endl; // if user enters any other symbol
goto abc;                                                                     // go to line labeled abc
}
                cout<<"Answer= "<<answer<<endl;         //display the Answer
                abc:cout<<"Do another calculation(y/n):";//ask for confirmation
                cin>>decision;                                          //take it as the decision

                if(decision=='n')                                        //if entered n, then break the loop
                break;                   

                else                                                           //if not, continue the loop
                continue;

}

cout<<"Thank you. Good buy!"<<endl;              //before exiting, display this message
return 0;                                                                 //return an integer and end the program

}

Now see the below screenshot which shows how the program responds during run time.

















I hope it was a good practice for you to have hands on experience on switch, if-else, while loop, break, continue and goto statements all in one such program.  Just try to make the requirements hard and improve your calculator little by little.