Basic Calculator

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.

Here is my code :

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
// Write a program that adds, subtracts, divides or multiples based on the users choice.
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    double fnum, snum, total;
    char operation;
    
    cout << "Please enter the first number: ";
    cin >> fnum;
    cout << "Please choose an operator (+ - * /)";
    cin.get(operation);
    cout << "Please enter the second number: ";
    cin >> snum;
    
    if (operation == '+'){
        fnum + snum = total; // LINE 29 ERROR
        cout << "Adding " << fnum << " + " << snum << " yields : " << total;
    }
    else if (operation == '-'){
        fnum - snum = total; // LINE 33 ERROR
        cout << "Subtracting " << snum << " from " << fnum << " yields : " << total;
    }
    else if (operation == '*'){
        fnum * snum = total; // LINE 37 ERROR
        cout << "Multiplying " << fnum << " by " << snum << " yields : " << total;
    }
    else{
        fnum / snum = total; // LINE 41 ERROR
        cout << "Dividing " << fnum << " by " << snum << " yields : " << total;
        }
    
    
    cin.get();
    cin.get();
    return 0;
}
        


Thanks for any help / insight you can offer.
You should do it the other way around:

1
2
3
4
5
6
7
8
9
//...
total = fnum + snum;
//...
total = fnum - snum;
//...
total = fnum * snum;
//...
total = fnum / snum;
//... 
Thank you so much master r0shi...

That solved it!

Where do I know your name from? is that something out of DBZ or?
you can make a switch case menu instead of using if..else...else if.....and so one..it is a bit more elegant.and safer,having the default option.
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.
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
#include <iostream>
using namespace 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.
Last edited on
Thanks fo the code Kyon,

I'm planning to implement square root and power functions.
Ah, in that case, you'd indeed be better of including cmath.
Topic archived. No new replies allowed.