I'm trying to write a program that prompts the user to enter a math expression (i.e 2*x + x*x) and a value of x. Then generate the value of y. My real question is: Is there a way to put the content of a string into the source code?
You can bring in a string of course but how are you going to evaluate the expression? You'll have to parse it into tokens and write your own code to do the math properly. Remember, simply replacing the variable x with what the user inputs will usually give you a wrong answer. Look at your example for an x value of 3: 2 * 3 + 3 * 3 = 27 but that's wrong. Using the proper operator precedence it should really be: (2 * 3) + (3 * 3) = 6 + 9 = 15.
Too bad that C (has) no eval(). Says the guy who's username is cnoeval!