fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. double num1, num2;
  6. char op;
  7. char choice;
  8.  
  9. do {
  10. // Input numbers and operator
  11. cout << "Enter first number: ";
  12. cin >> num1;
  13.  
  14. cout << "Enter an operator (+, -, *, /): ";
  15. cin >> op;
  16.  
  17. cout << "Enter second number: ";
  18. cin >> num2;
  19.  
  20. // Perform calculation
  21. switch (op) {
  22. case '+':
  23. cout << "Result: " << num1 + num2 << endl;
  24. break;
  25. case '-':
  26. cout << "Result: " << num1 - num2 << endl;
  27. break;
  28. case '*':
  29. cout << "Result: " << num1 * num2 << endl;
  30. break;
  31. case '/':
  32. if (num2 != 0)
  33. cout << "Result: " << num1 / num2 << endl;
  34. else
  35. cout << "Error: Cannot divide by zero!" << endl;
  36. break;
  37. default:
  38. cout << "Error: Invalid operator!" << endl;
  39. }
  40.  
  41. // Ask if the user wants to continue
  42. cout << "Do you want to perform another calculation? (y/n): ";
  43. cin >> choice;
  44.  
  45. } while (choice == 'y' || choice == 'Y');
  46.  
  47. cout << "Calculator closed. Goodbye!" << endl;
  48.  
  49. return 0;
  50. }
  51.  
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Enter first number: Enter an operator (+, -, *, /): Enter second number: Error: Invalid operator!
Do you want to perform another calculation? (y/n): Calculator closed. Goodbye!