# First C++ Program : Simple Calculator

Usage :

## Multiplication

Multipliction is simple, Enter your first digit in (eg. 1) Enter your operator (x) Enter your second digit in (eg. 1)

## Division

Division is also simple, Enter your first digit in (eg. 1) Enter your operator (/) Enter your second digit in (eg. 1)

## Subtraction

Enter your first digit in (eg. 1) Enter your operator (-) Enter your second digit in (eg. 1)

# Code:

#include <iostream>
using namespace std;

// Function Declaration
int multiply(int firstnumber, int secondnumber, char operation);
int square(int firstnumber, char operation);
int cube(int firstnumber, char operation);
int divide(int firstnumber, int secondnumber, char operation);
int subtract(int firstnumber, int secondnumber, char operation);

int main(){
// Variable Declaration :
int number1;
int number2;
char operation;

// User Input :
cout << "Enter the first number" << endl;
cin >> number1;

cout << "Enter the operator" << endl;
cin >> operation;

cout << "Enter the second number (leave blank for cube and square)" << endl;
cin >> number2;

cout << "This is your sum." << endl;
cout << number1 << operation << number2 << endl;

// Calculating :

int result;

char m = 'x';
char sq = 's';
char c = 'c';
char d = '/';
char s = '-';

if (operation == m) {
result = multiply(number1, number2, operation);
} else if (operation == sq) {
result = square(number1, operation);
} else if (operation == c) {
result = cube(number1, operation);
} else if (operation == d) {
result = divide(number1, number2, operation);
} else if (operation == s) {
result = subtract(number1, number2, operation);
} else {
cout << "Wrong operator input. Try again" << endl;
return 2;
}

// Outputing Result :
cout << "The result is : " << result << endl;
return 0;
}

// Function Definition
int multiply(int firstnumber, int secondnumber, char operation) {
return firstnumber * secondnumber;
}
int square(int firstnumber, char operation) {
return firstnumber * firstnumber;
}
int cube(int firstnumber, char operation) {
return firstnumber * firstnumber * firstnumber;
}
int divide(int firstnumber, int secondnumber, char operation) {
return firstnumber / secondnumber;
}
int subtract(int firstnumber, int secondnumber, char operation) {
return firstnumber - secondnumber;
}

## Avoid using using namespace std;

Find more details here why using namespace std; is considered bad practice

## Use '\n' for adding newline

std::endl doesn't just add newline but it also flushes the stream. More details about it std::endl vs '\n'

## Refactor into functions

Make a function for calculation. What if you'd want to calculate twice? By making it into a function you can simply call it as needed.

Example:

int calculate(...){ ... }

int main(){
int result1 = calculate(2, 3, 'x');
int result2 = calculate(2, 'c');
}

## Leverage STL

• std::divides for division.
• std::minus for subtraction.
• std::multiplies for multiplication.
• Example usage:
std::minus<int>{}(10, 9)  // -> 10 - 9 is performed
std::divides<int>{}(3, 2) // -> 3 / 2 is performed

## Remove unnecessary parameters from the function.

char operation parameter is never used. It doesn't serve any purpose and can be removed.

## switch-case

You could use switch for mapping char to operation. And to avoid writing operation == /*op*/ for each operation in every if and elif.

switch(op){
case 'x':
return std::multiplies<int>{}(num1, num2);
case '/':
return std::divides<int>{}(num1, num2);
case '-':
return std::minus<int>{}(num1, num2);
case 'c':
return cube(num1);
case 's':
return sqaure(num1);
default:
/* Handle when op is other than [-, /, x, c, s] */
};,