c++ simple calculator
- User
c++ simple calculator
- Assistant
Below is an example of a simple calculator in C++ that performs basic arithmetic operations like addition, subtraction, multiplication, and division. It accepts two operands and an operator from the user and then performs the operation based on the given input.
#include <iostream> using namespace std; int main() { double num1, num2; char op; double result; cout << "Enter first number: "; cin >> num1; cout << "Enter operator (+, -, *, /): "; cin >> op; cout << "Enter second number: "; cin >> num2; switch (op) { case '+': result = num1 + num2; break; case '-': result = num1 - num2; break; case '*': result = num1 * num2; break; case '/': if (num2 != 0.0) { result = num1 / num2; } else { cerr << "Error: Division by zero!" << endl; return 1; } break; default: cerr << "Error: Invalid operator!" << endl; return 1; } cout << "Result: " << result << endl; return 0; }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.