OK I am wondering Can you make a variable hold an operator.
Such as say +, -, / etc...

And if so can that variable be used to replace an operator in an equation?

for instance

char a = "+";
int b = (2 a 3);

thoguh that doesn't work itself i think it conveys what i am looking for.

Recommended Answers

All 6 Replies

Member Avatar for iamthwee

No.

What are you trying to do.

you can use it in a switch statement

int b = 1;
int c = 2;
int d = 0;
switch(a)
{
   case '+':  d = b * c; break;
   case '-':   d = b - c; break;
   case '/':   d = b / c; break;
  // etc
}

OK I am wondering Can you make a variable hold an operator.
Such as say +, -, / etc...

And if so can that variable be used to replace an operator in an equation?

for instance

char a = "+";
int b = (2 a 3);

thoguh that doesn't work itself i think it conveys what i am looking for.

You can do this to have the same effect:

#define a +

int b = (2 a 3);

But 'a' is not really a variable here.
And I see no reason why you would want to do that...

You can do this to have the same effect:

#define a +

int b = (2 a 3);

But 'a' is not really a variable here.
And I see no reason why you would want to do that...

You might want to do something like that when writing a calculator. for example, if you enter "2 * 3" then your program needs to figure out what to do.

You might want to do something like that when writing a calculator. for example, if you enter "2 * 3" then your program needs to figure out what to do.

That is pretty much my reasoning is calculations with the equation from an outside source. I did think of using switches/loops/etc... but they are all rather convoluted ways to do something that would be so simple. Man the more i am learning of C++ the more glaring flaws i am seeing.

Are there any compliers with additions that will fix some things, simple things like length_of(Array), treat_as_value(char). So many things that would make the programing both easier and less taxxing on memory and processor time. And if it was compiled to exe it should retain portability or am i missing something?

I don't know if not is there a langauge I should try that doesn't have such drastic oversights?

Are there any compliers with additions that will fix some things, simple things like length_of(Array), treat_as_value(char).

Not in C or C++ languages.

So many things that would make the programing both easier and less taxxing on memory and processor time. And if it was compiled to exe it should retain portability or am i missing something?

C/C++ is probably the least taxing of all computer languages except assembly and machine code. Other languages, such as basic, let you do some things a lot easier but the executable programs are generally larger and slower.

If you want to get reall deep into parsing equations then try YACC (Yet Another Compiler Compiler)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.