Skip to content

Instantly share code, notes, and snippets.

@coder-ralph
Created January 16, 2023 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coder-ralph/3232d30483d936e7b56c74e0ad7541fe to your computer and use it in GitHub Desktop.
Save coder-ralph/3232d30483d936e7b56c74e0ad7541fe to your computer and use it in GitHub Desktop.
Basic calculator using switch cases in C++
# include <iostream>
using namespace std;
int main() {
char op;
float num1, num2;
cout << "Enter operator: +, -, *, /: ";
cin >> op;
cout << "Enter two operands: ";
cin >> num1 >> num2;
switch(op) {
case '+':
cout << num1 << " + " << num2 << " = " << num1 + num2;
break;
case '-':
cout << num1 << " - " << num2 << " = " << num1 - num2;
break;
case '*':
cout << num1 << " * " << num2 << " = " << num1 * num2;
break;
case '/':
cout << num1 << " / " << num2 << " = " << num1 / num2;
break;
default:
cout << "Error! operator is invalid";
break;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment