hey. i would really appreciate some help on an assignment i've been struggling with.
i am supposed to:
Write a calculator program. Each time the program starts, reset the current value to zero. The user will then enter an arithmetic operator (either +, -, * or /) and then a number (double). Your program will respond by performing that operation on the current value and displaying the result. The program will stop executing when the user enters a ‘Q’ for the operation. For an extra challenge (not required), make sure the program does not try to divide by zero (see the example below for what the program should display in that case.) YOU MUST USE A DO…WHILE AS THE MAIN LOOP FOR THIS PROGRAM.
so far i have:
#include <iostream>
#include <string>
using namespace std;
int result;
char sign;
int value;
int main()
{
result = 0;
// Loop forever (or till we hit the break statement)
do
{
cout << "Current value is " << result << '\n';
cout << "Please enter an operation +, -, *, / <'Q' to quit>: ";
cin >> sign;
cout << "Please enter a number: ";
cin >> value;
if (sign != '+' && sign != '-' && sign != '*' && sign != '/')
{
cout << "Unknown operator " << sign << '\n';
}
else
{
if (sign = '+')
{
result += value;
}
else
{
if (sign = '-')
{
result -= value;
}
else
{
if (sign = '*')
{
result *= value;
}
else
{
if (sign = '/')
{
result /= value;
}
}
}
}
}
}
while (1);
return (0);
}
a problem i am having is that it only adds numbers and doesn't perform the other operations. can anyone please help?
thanks!