belongs to collection: Switch case C++ programs with an examples
I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.
#include <iostream> using namespace std; int main(){ char mathoperator; double firstNumber, secondNumber; //Reading mathoperator from user cout<<"Enter an operator (+, -, *,): "; cin>>mathoperator; //Reading operands from user cout<<"Enter two operands: "; cin>>firstNumber; cin>>secondNumber; switch(mathoperator) { cout << firstNumber << " + " << secondNumber << " = " << firstNumber+secondNumber; break; case '-': cout << firstNumber << " - " << secondNumber << " = " << firstNumber-secondNumber; break; case '*': cout << firstNumber << " * " << secondNumber << " = " << firstNumber*secondNumber; break; case '/': cout << firstNumber << " / " << secondNumber << " = " << firstNumber/secondNumber; break; // operator doesn't match any case constant (+, -, *, /) default: cout<<"Error! please enter correct operator"; } return 0; }
Result:
Enter an operator (+, -, *,): *
Enter two operands: 10
50
10 * 50 = 500
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.
Result:
Enter an operator (+, -, *,): *
Enter two operands: 10
50
10 * 50 = 500
need an explanation for this answer? contact us directly to get an explanation for this answer