Basically my question is how can I assign an operator, + - * /, to a variable and then use it within an equation using that variable? What I eventually want to do is generate a random number from 1-4 and have an operator assigned to the 1 2 3 4 index of an array and then have the operator plugged into the equation using the array.
Here is an example in code that obviously doesn't work.
Based on the code, the equation would be 6 + 2
Thanks for any help :)
C++ has the concept of operator overloading, but to understand it, you must first understand the concept of classes and object oriented programming. In your case, you should rather write functions convering each operator role or use switch or both:
1 2 3 4 5 6 7 8 9 10 11
int add( int first, int second) {
return first + second
};
char op;
switch(op)
{
case'+':
answer = add(6, 2);
break;
//etc
}