So I am trying to make a program that does math operations based on user input, however I am running into an issue with trying to set the math operator based on what they give.
Function:
const operator(int val)
{
if (val == 1)
{
return +;
}
if (val == 2)
{
return -;
}
}
With a main code looking something like this
scanf("%d", val)
output = 4 operator(val) 2
printf("%d", output)
Is there a variable type that I can use in place of an operator? If not is there a way to make a variable/function reference a defined macro?
For example:
#define plus +
then reference the macro in the code?
Finally I am aware I could have if cases for each input, however this scales poorly for something like,
output = 2 operator(val) 5 operator(val) 7 operator(val) 3
which would require 64 if statements I think to make it work.
Thank you for reading, I am at my wits end on this.