fork download
  1. #include <cctype>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. float addition(float number1, float number2)
  6. {
  7. return number1 + number2;
  8. }
  9. float subtraction(float number1, float number2)
  10. {
  11. return number1 - number2;
  12. }
  13. float multiplication(float number1, float number2)
  14. {
  15. return number1 * number2;
  16. }
  17. float division(float number1, float number2)
  18. {
  19. return number1 / number2;
  20. }
  21.  
  22. bool ask(const std::string& question)
  23. {
  24. std::cout << question;
  25. char answer;
  26. do {
  27. std::cin >> answer;
  28. answer = tolower(answer);
  29. if(answer != 'y' && answer != 'n') std::cout << "invalid input\n";
  30. } while(answer != 'y' && answer != 'n');
  31. return answer == 'y';
  32. }
  33.  
  34.  
  35. void menu()
  36. {
  37. float num1;
  38. float num2;
  39. int choice;
  40. std::cout << "\nCalculator function enabled. \n"
  41. "1: addition 2: subtraction 3: multiplication 4: division\n"
  42. "Your selection: ";
  43. std::cin >> choice;
  44. std::cout << '\n';
  45. switch(choice) {
  46. case 1: std::cout << "You are doing an Addition\n"; break;
  47. case 2: std::cout << "You are doing a Subtraction\n"; break;
  48. case 3: std::cout << "You are doing a multiplication\n"; break;
  49. case 4: std::cout << "You are doing a division\n"; break;
  50. default:
  51. std::cout << "Invalid operation\n";
  52. return;
  53. }
  54. std::cout << "First Number: ";
  55. std::cin >> num1;
  56. std::cout << "Second Number: ";
  57. std::cin >> num2;
  58. if(choice == 4 && num2 == 0) {
  59. std::cout << "Inappropriate Operation\n";
  60. return;
  61. }
  62. std::cout << "Your answer is: ";
  63. switch(choice) {
  64. case 1: std::cout << addition(num1, num2) << '\n'; break;
  65. case 2: std::cout << subtraction(num1, num2) << '\n'; break;
  66. case 3: std::cout << multiplication(num1, num2) << '\n'; break;
  67. case 4: std::cout << division(num1, num2) << '\n'; break;
  68. }
  69. }
  70.  
  71.  
  72.  
  73. int main()
  74. {
  75. do {
  76. menu();
  77. } while(ask("\nDo you want to do another operation? Y or N?"));
  78. }
Success #stdin #stdout 0s 3236KB
stdin
3 6 8 y 4 1 0 n
stdout
Calculator function enabled. 
1: addition 2: subtraction 3: multiplication 4: division
Your selection: 
You are doing a multiplication
First Number: Second Number: Your answer is: 48

Do you want to do another operation? Y or N?
Calculator function enabled. 
1: addition 2: subtraction 3: multiplication 4: division
Your selection: 
You are doing a division
First Number: Second Number: Inappropriate Operation

Do you want to do another operation? Y or N?