Question
In C++ write a program to model a simple calculator. Each data line should consist of the next operation to be performed from the list
In C++
write a program to model a simple calculator. Each data line should consist of the next operation to be performed from the list below and the right operand. Assume the left operand is the accumulator value (initial value of 0). You need a function scan_data with two output parameters that returns the operator and right operand scanned form a data line. You need a function do_next_op that performs the required operation, do_next_op has two input parameters (the operator and operand) and one input/output parameter (the accumulator). The valid operators are: + add - subtract * multiply / divide ^ power (raise left operand to power of right operand) q Quit Your calculator should display the accumulator value after each operation. A sample Run follows.
+ 10.0 Result so far is 10.0
/0 Error: division by zero
Result so far is 10.0 / 2.0 Result so far is 5.0
&2 Error: invalid operand
Result so far is 5.0 ^2 Result so far is 25.0
q0 Final result is 25.0
This is my coding
#include #include using namespace std;
double scan_data(char&, double&); void do_next_op(char, double, double&);
int main() { double accum = 0.0; char op; double num; accum = scan_data(op, num); do_next_op(op, num, accum); /*while(op != 'q') { do_next_op(op, num, accum); cout << "Result so far is " << accum << endl; scan_data(op, num); } */ return 0;
}
double scan_data(char& op, double& num) { double accum = 0.0;
while(op != 'q') {
cin >> op; cin >> num;
do_next_op(op, num, accum);
if(op != 'q') { cout << "Result os far is " << accum << endl;
} else { cout << "Final result is " << accum << endl; }
}
return accum; }
void do_next_op(char op, double num, double& accum) { if(num == 0) { cout << "Error: division by zero" << endl; } if(op == '+') { accum += num; } else if(op == '-') { accum -= num; } else if(op == '*') { accum *= num; } else if(op == '/') { accum /= num; } else if(op == '^') { accum = pow(accum, num); } else { cout << "Error: invalid operand" << endl; }
}
When I divid it by zero I get "inf" in the output.
So can anyone correct my coding to get the output as the sample pleasse.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started