I wanted to do a calculator with c++, and I did not wish my calculator to do only 2 number calculations, so I "made" 2 operator calculator. Also, I want to learn if there is a way to make this without two switch statements to make it easier for machines and programmers to read.
#include <iostream>
int top1 ;
int main()
{
char operatr1, operatr2;
float num1, num2, num3, num4 ;
std::cout << "Please choose the first operator ( +, -, *, /):";
std::cin >> operatr1 ;
std::cout << "Please choose the second operator ( +, -, *, /):";
std::cin >> operatr2;
std::cout << "Please enter three numbers: " << std::endl;
std::cin >> num1 >> num2 >> num3;
switch (operatr1) {
case '+':
int top1{ num1 + num2 };
break;
case '-':
int top1{ num1 - num2 };
break;
case '*':
int top1{ num1 * num2 };
break;
case '/':
int top1{ num1 / num2 };
break;
default:
std::cout << "Error! The operator is not correct" << std::endl << "The operators are ( +, -, *, / ).";
break;
}
switch (operatr2) {
case '+':
int top2{ top1 + num3 };
std::cout << "The answer is:" << " " << top2;
break;
case '-':
int top2{ top1 - num3 };
std::cout << "The answer is:" << " " << top2;
break;
case '*':
int top2{ top1 * num3 };
std::cout << "The answer is:" << " " << top2;
break;
case '/':
int top2{ top1 / num3 };
std::cout << "The answer is:" << " " << top2;
break;
default:
std::cout << "Error! The operator is not correct" << std::endl << "The operators are ( +, -, *, / ).";
break;
}
}