fork download
  1. #include <iostream>
  2. using namespace std;
  3. double add(double x, double y) { return x + y;}
  4. double mul(double x, double y) { return x * y;}
  5. double sub(double x, double y) { return x - y;}
  6. double divi(double x, double y) {
  7. if (y != 0) {
  8. return x / y;
  9. }
  10. else {
  11. cout << "Error : division by zero";
  12. return 0;
  13. }
  14. }
  15.  
  16. int main () {
  17. double price1, price2;
  18. int operationChoice;
  19. cout << "Enter the price of the first item:";
  20. cin >> price1;
  21. cout << "Enter the price of the second item:";
  22. cin >> price2;
  23. cout << "Choose the operation (1 for add, 2 for mul, 3 for sub, 4 for divi) ";
  24. cin >> operationChoice;
  25. if (operationChoice == 1) {
  26. cout << add(price1, price2);
  27. }
  28. else if (operationChoice == 2) {
  29. cout << mul(price1, price2);
  30. }
  31. else if (operationChoice == 3) {
  32. cout << sub(price1, price2);
  33. }
  34. else if (operationChoice == 4) {
  35. cout << divi(price1, price2);
  36. }
  37. else {
  38. cout << "Invalid choice, try again";
  39. }
  40. return 0;
  41. }
  42.  
Success #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
Enter the price of the first item:Enter the price of the second item:Choose the operation (1 for add, 2 for mul, 3 for sub, 4 for divi) Invalid choice, try again