fork(1) download
  1. //Calculator example
  2.  
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. char oper;
  9.  
  10. float a, b;
  11. // allows user to enter operand
  12.  
  13. cout << "Enter two operands: ";
  14. cin >> a >> b;
  15.  
  16. // allows user to enter operand +, -, *, /
  17.  
  18. cout << " Enter operator: ";
  19. cin >> oper;
  20.  
  21. // Switch statement
  22.  
  23. switch (oper)
  24. {
  25. // if operator is '+'
  26. case '+':
  27. cout << a + b;
  28. break;
  29.  
  30. // if operator is '-'
  31. case '-':
  32. cout << a - b;
  33. break;
  34.  
  35. // if operator is '*'
  36.  
  37. case '*':
  38. cout << a * b;
  39. break;
  40.  
  41. // if operator is '/'
  42.  
  43. case '/':
  44. cout << a/b;
  45. break;
  46.  
  47. // if any other operator display error
  48. default:
  49. cout << "Error! Incorrect operator";
  50. break;
  51.  
  52. }
  53.  
  54. return 0;
  55. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Enter two operands:  Enter operator: Error! Incorrect operator