Hi, I was having problem with my program. It still have some error when I typed in 5.0 and it only showed up 5 or I typed some other words that wasn't digit and it will be a big error. Can someone tell me what should I do to fix this?
Here's my homework assignment:
Write a Program that models a simple calculator. Each data entry line should consist of a valid operator (from the list below), and the right-hand operand. Assume that the left-hand operand is the accumulated value in the calculator, with an initial value of 0.0.
Acceptable operators:
+ ..Add
- ..Subtract
* ..Multiply
/ ..Divide
^ ..Power (raise left operand to the power of the right operand)
q or = ..Quit
Your Calculator should display the accumulated value after each operation. A sample run might be:
+ 5.0
Result so far is 5.00
^ 2
Result so far is 25.00
/ 2.0
Result so far is 12.50
Q 0
The final result is 12.50
Include (define and call) at least THREE functions:
* a function that displays instructions to the user.
* a function do_next_op() that has 3 input parameters (the operator, the operand, and the current accumulated value), and returns the new value for the accumulated value. An alternative implementation may use 2 input parameters (operator and operand), and 1 input/output parameter (the accumulated value)
* at least one other function - of your choice! Make sure that it does something useful.
Here's my program code:
Code:
#include <iostream>
#include <cmath>
using namespace std;
void instruction ();
float do_next_op ( float&, float&, char );
int main()
{
instruction(); // displays instruction
float total;
float newentry;
char op;
total = 0;
// initialisation
cin >> op;
while (op != 'Q' && op != 'q' && op != '=')
{
cin >> newentry;
do_next_op (total, newentry, op);
cin >> op;
}
cout << "the final result is " << total << endl;
system ("pause");
return 0;
}
void instruction()
{
}
float do_next_op ( float &total, float &newentry,char op)
{
switch (op)
{
case '+': total = total + newentry;
break;
case '-': total = total - newentry;
break;
case '*': total = total * newentry;
break;
case '/': total = total / newentry;
if (newentry == 0)
{
cout << "divide by zero is unexecutable" << endl;
}
break;
case '^': total = pow (total,newentry);
break;
default : cout << " Unacceptable Operator(" << op << ")" << endl;
}
cout << "result so far is " << total << endl;
cout << endl;
return (total);
}