Today we are going to show you how to make a simple calculator using c++ . We would discuss complex calculator in the next edition.

This calculator has been made using one of the basic and most used c++ syntax “the switch statement”. Switch statement in layman terms is a syntax which replaces many conditional statements of “if” into one single statement.

So first lets discuss the working of switch:

The syntax of switch is shown below:

switch(expression/charactor)
{ case constant-expression : /*please note that the expression must be a constant*/
{statement(s);
}break;                      /* optional */
case constant-expression :
statement(s);
break;                      /* optional */                                                              /* you can have any number of case statements */
default :                   /* Optional */
statement(s);
}                           /* end of the switch*/
 

Here the break is used to get out of the loop after the statement is executed if the break statement is not introduced it will execute the next case statement which will make the output wrong.

The next aspect that we have introduced in this programme is the first step to become a “professional” in coding.If u observe the main() is the smmalest component in the programme,the main advantage the programmer gets is the programme is made more simpler,by defining all the functions outside the main().

In order to help you to increase the depth in coding (which is our policy) in this edition we have introduced two new syntax

  1. exit(0); -THis syntax is used to terminate the program. Its defined in both STDLIB.H and PROCESS.H

CompuCoder TIP:

Always use the function getch() before using exit(0). Else the user cannot understand what the hell happened (lol).

2. clrscr(): This syntax is used to clear the output screen before running the programme. Its defined in CONIO.H header file.

Now here is the program for a calculator:

#include
 #include
 #include int a,b,c ;                  //global variables
void add(int a,int b)                  //adding function
 {c=a+b;
 cout<<"the sum is \t"<<c;
 }
 void sub(int a,int b)                 //subtracting fuction
 {c=a-b;
 cout<<"the difference is:\t "<<c;
 }
 void mult(int a,int b)                //multiplication function
 {c=a*b;
 cout<<"the product is:\t"<<c;
 }
 void div(int a, int b)                //division function
 {if(b==0)           //in the absence of this statement a "math error" occurs and the programme is terminated automatically
 {cout<<"\ndivision not possible" ;               //  "\n" its the new line key
 getch();
 exit(0);          //exit function
 }
 c=a/b;
 cout<<"the quotient is:\t"<<c;
 }
void cal(char ch)                                                     //calculator function
 { switch(ch)
 { case '1': add(a,b);
 break;
 case '2':sub(a,b);
 break;
 case '3':mult(a,b);
 break;
 case '4':div(a,b);
 break;
 case '5': exit(0); //no need for break beacause the program is terminated
 break;
 default: {cout<<"invalid entry " ;
 exit(0);
 }
 } //end of switch
 }// end of function
void main() //main prgm
 {clrscr(); //clrscr() used for clear screen
 cout<<"ENTER 1 FOR ADDING \n ENTER 2 FOR SUBTRACTION \n ENTER 3 FOR MULTIPLICATION \n ENTER 4 FOR DIVISION \n"; char ch; cin>>ch;
 if(ch>'4') //for preventing invalid entries
 {cout<<"invalid entry";
 getch();
 exit(0);
 }
 cout<<"\n";
 cout<<"ENTER THE FIRST OPERAND\n"; cin>>a;
 cout<<"ENTER THE SECOND OPERAND\n"; cin>>b;
 cal(ch);
 getch();
 }//end of main()

 OUTPUT:

1

This is the welcome screen and we have to enter the choice if a number otherthan the choice is entered it will show invalid entry. Now we enter 3 for multiplication.

1

Now we entered the operators and now wait for the result..
1

Now checkout the answer

Friends you can now further develop or improvize this programme by making it to end only when the user wants it,by using a “while” or “if else”…

So happy coding………..

Advertisements