5
Posted December 13, 2012 by Victor in C/C++ Programming
 
 

How to Program a Simple Calculator in C++

cac
cac

Have you ever though of coding a very simple calculator in C++? It  is true that so far, we have learned so much in C++. We have learnt of functions, variables, functions and conditional statements. All that and much more is in our C++ guide category. We have created a very simple calculator to apply most of the knowledge you have learned and to show you how C++ can be used in creating functioning programs.

In our simple calculator, we have done the blocks of codes inside functions to make work easier. The Main () function has finally been used so as to execute the program. We have also used the switch statements instead of the if/else conditions because of their ease to execute and nest. If you have followed our previous guides, it will not be difficult following how this simple calculator in C++ works.

Here is our very simple calculator:


#include <iostream>
 using namespace std;

//Operations Functions For Your Calculator

void multiply()
 {
   float x;
   float y;
   float z;
   cout << "\n....MULTIPLICATION IN PROGRESS...\n\n\n";

   cout << "Please enter the first number\n:__";
   cin >> x;
   cout << "\n\nPlease enter the second number\n:__";
   cin >> y;

   z = x * y;

   cout << "\n\n The answer to your multiplication is:__" << z;
   cout << "\n\n";
 }

void addition()
 {
   float a;
   float b;
   float c;
   cout << "\n....ADDITION IN PROGRESS...\n\n\n";

   cout << "Please enter the first number\n:__";
   cin >> a;
   cout << "\n\nPlease enter the second number\n:__";
   cin >> b;

   c = a + b;

   cout << "\n\nThe answer to your addition is:__" << c;
   cout << "\n\n";

 }

void division()
 {
   float a;
   float b;
   float c;
   cout << "\n....DIVISION IN PROGRESS...\n\n\n";

   cout << "Please enter the first number\n:__";
   cin >> a;
   cout << "\n\nPlease enter the second number\n:__";
   cin >> b;

   c = a/b;

   cout << "\n\nThe answer to your division is:__" << c;
   cout << "\n\n";

 }

void subtraction()
 {
   float a;
   float b;
   float c;
   cout << "\n....SUBTRACTION IN PROGRESS...\n\n\n";

   cout << "Please enter the first number\n:__";
   cin >> a;
   cout << "\n\n Please enter the second number\n:__";
   cin >> b;

   c = a-b;

   cout << "\n\nThe answer to your substration is:__" << c;
   cout << "\n\n";

 }

void calculator()
 {
   int calculator;
   cout << "\n....VERY SIMPLE CALCULATOR...\n\n\n";
   cout << "\n Please Select What You Want to do:\n\n1. Add Numbers\n\n";
   cout << "2. Multiply Numbers\n\n3. Subtract Numbers\n\n4. Divide Numbers\n\n:__";
   cin >> calculator;

   switch(calculator)
   {
     case 1 :
       addition();
       break;

     case 2 :
       multiply();
       break;

     case 3 :
       subtraction();
       break;

     case 4 :
       division();
       break;
     default :
       cout << "\n\n Please Enter Valid Numbers\n\n";
       break;
   }
 }

int main()
{
   int b;
   calculator();
   cout << "\n\nDo you wish to perform another operation?\n\n 1. YES \n\n 2. NO \n\n:___";
   cin >> b;

   switch(b)
   {
     case 1:
       calculator();
       break;

     case 2 :
       cout << "\n\n Thanks For Using Our Calculator\n\n";
       break;
   }

   return 0;
 }


Victor