PDA

View Full Version : Using a variable as an operator




andrewheard
Jan 28, 2008, 03:47 PM
Hi,
I wrote a little C program for my programming class and it asks the user what operation they would like to perform (+, -, /, *, %). I currently just have if statements that say if its + then value1 + value2, etc. Is it possible to use the variable as an operator so I don't need all the if statements or am I better off leaving it as is?

Thanks for your time,

Andrew



iSee
Jan 28, 2008, 03:55 PM
Hi,
I wrote a little C program for my programming class and it asks the user what operation they would like to perform (+, -, /, *, %). I currently just have if statements that say if its + then value1 + value2, etc. Is it possible to use the variable as an operator so I don't need all the if statements or am I better off leaving it as is?

Thanks for your time,

Andrew

C doesn't include a way to execute dynamically generated code, so you're better off leaving it as is.

andrewheard
Jan 28, 2008, 04:01 PM
Sounds good. Makes my life easy.