#include // Write a program that implements a simple integer // calculator. // // Ask the user to enter two integers. These will be the // left-hand and right-hand operands. // // Then ask the user to select one of the following // operations: // // 1. Addition // 2. Subtraction // 3. Multiplication // 4. Division // // If the user enters any integer other than 1, 2, 3 or 4, // print a message indicating that the selection is invalid. // // For addition, subtraction and multiplication, print the // complete calculation. // // For example, if the two operands are 7 and 12, the // corresponding outputs would be // // 7 + 12 = 19 // 7 - 12 = -5 // 7 x 12 = 84 // // For division, use integer division and also print the // remainder if it is nonzero. For example, // // 7 / 12 = 0 with a remainder of 7 // // If the remainder is zero, do not mention the remainder: // // 20 / 5 = 4 // // Remember that division by zero is undefined. If the // user selects division and the right-hand operand is // zero, print an appropriate error message instead of // attempting the division. // Function declarations int main(); // Function definitions int main() { return 0; } // Hint: // // Use a cascading conditional statement to determine // which operation the user selected: // // if ( choice == 1 ) { // ... // } else if ( choice == 2 ) { // ... // } else if ( choice == 3 ) { // ... // } else if ( choice == 4 ) { // ... // } else { // ... // } // // The final 'else' handles every integer other than // 1, 2, 3 and 4. // // Division requires an additional conditional statement. // // Before calculating // // m/n // // or // // m%n, // // what must you first determine about 'n'? // Here is one solution. // // Notice that not every detail of the user interface was // specified by the problem. // // For example, we provide the user with a menu: // // Enter 1 for addition, // 2 for subtraction, // 3 for multiplication, and // 4 for division: // // A shorter prompt such as // // Enter 1-4 to add, subtract, multiply or divide: // // would also satisfy the requirements. // // On the other hand, a prompt such as // // Enter a number: // // would be a poor choice because it does not tell the user // what input is expected or what the available choices // mean. // // As an engineer, you should consider the user interface // when designing software. A program should make it clear // what information the user is expected to provide. /* int main() { int m{}; std::cout << "Enter the left-hand operand: "; std::cin >> m; int n{}; std::cout << "Enter the right-hand operand: "; std::cin >> n; int choice{}; std::cout << "Enter 1 for addition," << std::endl; std::cout << " 2 for subtraction," << std::endl; std::cout << " 3 for multiplication, and" << std::endl; std::cout << " 4 for division: "; std::cin >> choice; if ( choice == 1 ) { std::cout << m << " + " << n << " = " << (m + n) << std::endl; } else if ( choice == 2 ) { std::cout << m << " - " << n << " = " << (m - n) << std::endl; } else if ( choice == 3 ) { std::cout << m << " x " << n << " = " << (m*n) << std::endl; } else if ( choice == 4 ) { // Before using either '/' or '%', make sure // that the divisor is not zero. if ( n == 0 ) { std::cout << "Division by zero is undefined." << std::endl; } else { std::cout << m << " / " << n << " = " << (m/n); // Print the remainder only if it is nonzero. if ( m%n != 0 ) { std::cout << " with a remainder of " << (m%n); } std::cout << std::endl; } } else { // If this block is reached, 'choice' was not // 1, 2, 3 or 4. std::cout << "Invalid operation: " << choice << std::endl; } return 0; } */