Write a C++ program( or C program): that will work with conditional statements t
ID: 3925014 • Letter: W
Question
Write a C++ program( or C program): that will work with conditional statements to implement simple calculator program.:
Input: Enter a math expression of the form : a op b, where a and b are operands and op is one of the following: +, -, *, / . Output: Given a valid expression, your program should clculate the result and reprint the entire expression and its result. [example: Enter precision:2 ., Enter expression: 3/2..... result: 3.00/2.00 = 1.50........ ]
Error checking: Your program should print an error under any of the following conditions: - any input that is incorrectly formatted therefore can not be read correctly using scanf(). nVals = scanf("%d %d %d"); - User tries to divide by zero. - the mathematical operator entered is not a valid operator. - the precision is not a valid value(must be>/ 0) ( must be greater than or equal to zero:0)
Explanation / Answer
main.cpp
# include <iostream>
using namespace std;
int main()
{
char op;
float num1,num2;
cout << "Enter operator either + or - or * or /: ";
cin >> op;
cout<<endl;
cout << "Enter two operands: ";
cin >> num1 >> num2;
cout<<endl;
switch(op) {
case '+':
cout <<endl<< num1 <<op<<num2<<" = "<<num1+num2;
break;
case '-':
cout <<endl<< num1<<op<<num2<<" = "<< num1-num2;
break;
case '*':
cout << endl<<num1<<op<<num2<<" = "<< num1*num2;
break;
case '/':
cout << endl<< num1<<op<<num2<<" = "<<num1/num2;
break;
default:
cout <<endl<< "Error! operator is not correct";
break;
}
return 0;
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.