How to Take Operator as Input in C++? Last Updated : 09 Feb, 2024 Comments Improve Suggest changes Like Article Like Report Operators are symbols that specify some kind of operation. In C++, we sometimes need to take operators as user input mainly to perform mathematical operations. In this article, we will learn how to take operators as user input in C++. Operators as Input in C++To take operators (like +,-,*,/ etc) as user input, we can use a char datatype to read the operator as a character using cin and then validate whether the entered operator is valid or not using a switch or conditional statements. C++ Program to Take Operator as InputThe below example demonstrates how we can take the operator as user input, validate it, and perform mathematical calculations. C++ // C++ program to take operator as input #include <iostream> using namespace std; int main() { // Declaring variables to store two numbers and the // operator double num1, num2; char op; // Get the first number from the user cout << "Enter the first number: "; cin >> num1; // Get the second number from the user cout << "Enter the second number: "; cin >> num2; // Get the arithmetic operator from the user cout << "Enter the arithmetic operator (+, -, *, /): "; cin >> op; // Declare a variable to store the result of the // operation double result; // Use a switch statement to perform the operation based // on the operator switch (op) { case '+': result = num1 + num2; break; case '-': result = num1 - num2; break; case '*': result = num1 * num2; break; case '/': // Check if the second number is not zero before // performing division if (num2 != 0) { result = num1 / num2; } else { cout << "Error: Division by zero." << endl; return 1; // Return an error code } break; default: cout << "Error: Invalid operator." << endl; return 1; // Return an error code } // Display the result of the operation cout << "Result: " << result << endl; return 0; } Output Enter the first number: 8 Enter the second number: 2Enter the arithmetic operator (+, -, *, /): /Result: 4 Comment More infoAdvertise with us Next Article How to Take Operator as Input in C++? L lunatic1 Follow Improve Article Tags : C++ Programs C++ cpp-input-output cpp-operator CPP Examples +1 More Practice Tags : CPPcpp-operator Similar Reads How to Read a Line of Input Text in C++? In C++, we often need to take input from the user by reading an input text line by line but the cin method only takes input till whilespace. In this article, we will look at how to read a full line of input in C++. For Example, Input: This is a text.Output: Entered Text: This is a text.Taking a Line 2 min read Take String as Input in C++ Strings are used to store the textual information. Taking a string as input is a very common operation used in almost all fields of programming. In this article, we will look at different ways to take string as input.The simplest way to take string as the user input is to use cin object. Let's take 2 min read How to Take Multiple Line String Input in C++? In C++, taking string input is a common practice but the cin is only able to read the input text till whitespace. In this article, we will discuss how to read the multiple line of text input in C++. For Example, Input:Enter Your Text: This is amultiline text.Output:You Have Entered:This is amultilin 2 min read How to Take Multiple Input from User in C++? In C++, we use cin when we want to take input from the user. We often also need to take more than one input at a time. In this article, we will learn how to take multiple inputs in C++. Take Multiple Inputs from a User in C++To take multiple inputs from users, we can repeatedly use the std::cin usin 2 min read How to Overload the Multiplication Operator in C++? In C++, the multiplication operator is a binary operator that is used to find the product of two numeric values. In this article, we are going to learn how to overload the multiplication operator for a class in C++. Overloading Multiplication Operator in C++C++ provides the functionality of operator 2 min read Like