C++ calculator that loops answers
May 10, 2020 at 7:19am UTC
I am trying to code a calculator that uses the result of the previous calculation as one of the inputs for the next calculation. However, when I run the code, it keeps telling me that lvalue is required as the left operand of the assignment, with the compiler telling me that the "/" in line 18 is the problem. Any corrections would be greatly appreciated.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#include <iostream>
#include <string>
using namespace std;
int result;
int value;
char sign;
int main()
{
result = 0;
// Loop forever (or till we hit the break statement)
do
{
cout << "Current value is " << result << endl;
cout << "Please enter an operation +, -, *, / <'Q' to quit>: " ;
cin >> sign;
cout << "Please enter a number: " ;
cin >> value;
if (sign = '+' || sign = '-' || sign = '*' || sign = '/' )
{
if (sign = '+' )
{
result += value;
}
else if (sign = '-' )
{
result -= value;
}
else if (sign = '*' )
{
result *= value;
}
else if (sign = '/' )
{ result /= value;
}
}
else if (sign = 'q' )
{
break ;
}
else
{
cout << "Unknown operator " << sign << endl;
}
}while (1);
return 0;
}
May 10, 2020 at 7:22am UTC
> if (sign = '+' || sign = '-' || sign = '*' || sign = '/')
Use ==, not = for comparing.
And put all your globals inside main.
1 2 3 4 5
int main ( ) {
int result;
int value;
char sign;
.. etc
Last edited on May 10, 2020 at 7:23am UTC
May 10, 2020 at 7:22am UTC
"=" = "becomes"
"==" = equals
Topic archived. No new replies allowed.