Calculator using the switch case – Computer Sir Ki Class


Lost your password?

Don't have an account ?
Register (It's FREE) ×


Code Learning #CPP#2059 siteicon   siteicon   siteicon  

Calculator using the switch case

A program that works like a calculator for two input numbers and works for most arithmetic operations like addition, subtraction, multiplication, division etc.

Learning Objectives

  • Learning to use switch-case for special symbols.
  • Tricky use of switch case for mathematical operations.

Source Code

TC++ #2059


Source Code

Run Output

Enter two numbers : 12.34 11.78
Enter operator (+,-,*,/) : *

Enter two numbers : 34.2 2.2
Enter operator (+,-,*,/) : /

Code Understanding

double a,b;
Here we are declaring variables for input values. We are taking them as double so that sufficiently large fractional values can also be operated upon.

char op;
In this variable we shall collect the operator symbol as character.

cout<<“Enter two numbers : “; cin>>a>>b;
Here we are collecting the uperands.

cout<<“Enter operator (+,-,*,/) : “; cin>>op;
Here we are getting the operator symbol from the user in a char data type variable.

switch (op)
This selects between the operator character provided by the user.

case ‘+’: cout <<a<<“+”<<b<<“=”<<a+b; break;
Here we are doing it for + operator. The operands we assume are to be used in the order as given by the user.

case ‘-‘: cout <<a<<“-“<<b<<“=”<<a-b; break;
case ‘*’: cout <<a<<“*”<<b<<“=”<<a*b; break;
case ‘/’: cout <<a<<“/”<<b<<“=”<<a/b; break;
Similarly all operators cases are handled.

default: cout <<“Error in input”;
This is used when operator symbol is not correctly given


  • The basic +,-,*,/ operators work on all number data types but the modulus operator (%) does not work on fractional data types like float and double. You are advised to use fmod() function of the math library.

Common Errors

  • Some students are not comfortable writing calculation operations in the cout chain. To avoid error one can also write the case expressions like this.double res;
    case ‘+’:  res=a+b;
    cout <<a<<“+”<<b<<“=”<<res; break;
    case ‘-‘:  res=a-b;
    cout <<a<<“-“<<b<<“=”<<res; break;
    and so on.

    Note that the res variable has been reused. This is possible as only one of the cases has to be used at any time.

Suggested Filename(s): calc-sc.cpp


sunmitra| Created: 31-Dec-2017 | Updated: 15-Sep-2018|

Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning 2D Array Memory Addressing Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Multi-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues