Hello everyone, I was wondering if I could get some help with a basic calculator I 'm working on.
The calculator should ask the user to enter a first number(declared as a double), a second number, then choose the operation which they wish to use.
I couldn't figure this out with switch statements(have yet to cover them, sort of charting into unknown waters right now) so I decided to try my luck with if/else statements. I've come to a halt now and can't figure out why.
My error log states the following : 29 non-lvalue in assignment , this also applies to lines 33, 37 and 41. I google'd the error in hopes of finding a solution but to no avail.
Mihay, do you think you could demonstrate how the switch statement would be implemented in this calculator? I just adding a "would you like to do another calculation (y/n)" statement in.
The switch statement was the first thing I looked at but I couldn't figure it out.
#include <iostream>
usingnamespace std;
int main()
{
double fnum = 0.0, snum = 0.0, total = 0.0;
char operation = '';
cout << "Enter the first operand: ";
cin >> fnum;
cout << "Enter the second operand: ";
cin >> snum;
cout << "Enter the operation (+ - * /): ";
cin.get(operation);
switch(operation)
{
case'+': total = fnum + snum; break;
case'-': total = fnum - snum; break;
case'*': total = fnum * snum; break;
case'/': total = fnum / snum; break;
default: cout << "Invalid input" << endl; return 1;
}
cout << fnum << operation << snum << "=" << total;
// insert pausing code here
return 0;
}
Note that in the current state your code is in, you do not need cmath and unless you are going to add functionality that does require it, you should not include it.