# Thread: Math Operations in a Variable?

1. ## Math Operations in a Variable?

Alright. I've been following the tutorials offered by this site, and I've done a whole slew of lessons. So I'm trying to combine most of them into one program, and I've chosen to make a basic calculator. I completed it, and it works nicely. It first began with integers, now I've allowed it to use decimals as well. So, here's the code for the calculator.

Code:
```#include <iostream>

using namespace std;

int main()
{
int input;
float function;

cout<<"Awesome Calculator is Awesome.\n\n";
do
{
cout<<"2 = Subtract\n";
cout<<"3 = Multiply\n";
cout<<"4 = Divide\n";
cout<<"5 = Quit\n";
cout<<"Selection: ";
cin>> input;
switch ( input ){
case 1:
cout<<"\nFirst number: ";
cout<<"--------------------------------------\n";
cout<<"--------------------------------------\n\n";
cin.get();
break;

case 2:
cout<<"\nFirst number: ";
cout<<"\nSubtracted by?: ";
cout<<"--------------------------------------\n";
cout<<"--------------------------------------\n";
cin.get();
break;

case 3:
cout<<"\nFirst number: ";
cout<<"\nMultiplied by?: ";
cout<<"--------------------------------------\n";
cout<<"--------------------------------------\n";
cin.get();
break;

case 4:
cout<<"\nFirst number: ";
cout<<"\nDivided by?: ";
cout<<"--------------------------------------\n";
cout<<"--------------------------------------\n";
cin.get();
break;

case 5:
cout<<"Quitting...";
break;

default:
cout<<"--------------------------------------\n";
cout<<"--------------------------------------\n";
break;
}
}while (input != 5);
}```
Now, there is nothing (that I know), that is wrong with this program. I've compiled it many times, and it works perfectly. I don't need help with fixing it, or anything like that. What I want to do, is to shrink the program down. To optimize it. I see a clear way that I can do it, but I need to know one thing: If it's possible to store math operations in a variable. For example:

A = +
B = -
C = /
D = *

Though in my program, it'll all be stored in one variable, but you get the idea. I've tried using the 'float' variable, though it doesn't work...Or I'm just using it incorrectly. So, here's a little snippit of what I tried: (I'll just use case 1, you can see the original above.)

Code:
```case 1:
cout<<"\nFirst number: ";
function = (+);
cout<<"--------------------------------------\n";
cout<<"--------------------------------------\n\n";
cin.get();
break;```
So I bolded the part that I'm really trying to get at. Basically, if I haven't managed to get it across, I want the +,-,*,/ in the variable "function". So in this example, function will be an addition operation, and in essence, it should add inputadd1 and inputadd2 together. But as I've done this wrong, it doesn't. So, is their a specific type of variable I must use, or am I just approaching this the wrong way?

Quick Edit: As I mentioned above, I have been reading the tutorials, and I just may have missed it. But I did do a quick search for it, and didn't stumble across anything. So thanks for any help. :]

2. First, its good that you realize the unnecessarily repeated code in the program and want to minimize it.

For "storing an operation in a variable", well, you pretty much are already doing that! Your mapping 1 to add, 2 to subtract, and so on. If your asking how to store the operator in a variable, and use that variable to perform the operation itself, then you cant do that. You, at some point, have to use a switch (or similar method) to say "for character operator '+', I tell the computer to add (+)". Here is some C++/pseudocode that might help:
Code:
```int main(void)
- get operation from user (as a "char" which represents the operator, rather than a number as you are now)
- get number 1 from user
- get number 2 from user
- print the result of 'eval' of the function
}

float eval(char operator, float num1, float num2)
{
- use switch statement on the operator to determine what operation to do
- make sure to prevent the case where op == '/' and num2 == '0'
- return the value
}```

3. If it's possible to store math operations in a variable. For example:

A = +
B = -
C = /
D = *
for built-in types, no, you cannot.

for class objects, or a custom function, you can store a method or function pointer, respectively.

what can work in all cases is a functor, where you encapsulate the operation you want to perform in a class.

i think you still have a while before you get there though, so i think the switch is the right choice from your current toolbox.

4. Well that's depressing. Thanks for the tips, though!