0

Let me explain what I want to do:

I want to change a expression like this: x * (2 + 3) - (y + 1)

to this: 5 * (2 + 3) - (6 + 1)

using this: instVar('x', 5); instVar('y', 6);

My problem is that I only get it to output the "original" expression but not the changed "modified" expression. I don't know why and I was hoping that you might point out to what I am doing wrong.

Here is my code:

void Expression::instVar(char var, int val)
{
    char vars[0];
    int vals[0];

    if(modifiedExpr == "")
    {
        modifiedExpr = originalExpr;
        vars[0] = var;
        vals[0] = val;
    }
    else
    {
        for(int i = 0; i < originalExpr.length(); i++)  //Searching for the var in the original expression
        {
            if(vars[i] == var)  //If the variable is found
            {
                modifiedExpr = originalExpr;    //Setting the expression to the original one so that x can be "replaced"
                vals[i] = val;  //Replace the variable with the value - Does not work :(((
                break;
            }
        }
    }

    //Testing the output if it works
    cout << modifiedExpr << endl;
}
MFJones
  • 39
  • 1
  • 2
  • 8
  • You're leaving out a bit. How about you include the relevant class variables? – Joel Cornett Aug 11 '14 at 14:10
  • 4
    Arrays cannot have zero size. Fix this and return. – awesoon Aug 11 '14 at 14:13
  • 2
    are you sure you wanted to define arrays with 0 elements? `char vars[0]` – glezmen Aug 11 '14 at 14:13
  • Why are you doing `modifiedExpr = originalExpr` inside the loop? – leemes Aug 11 '14 at 14:13
  • You're looking for this http://snawaz.github.io/foam/expression.html – Nawaz Aug 11 '14 at 14:13
  • @Nawaz I totally don't think so. He wants to modify expression **strings**, not evaluate them. – leemes Aug 11 '14 at 14:17
  • You're not modifying `modifiedExpr` except when you assign `originalExpr` to it, so that's what it will print. The `vars` and `vals` array don't seem to be doing anything at all apart from being invalid (these are also local to the function and are thrown away when it returns - I suspect you intend them to be members). – molbdnilo Aug 11 '14 at 15:01

2 Answers2

0

This:

            vals[i] = val;  //Replace the variable with the value - Does not work :(((

Doesn't work because the left hand size is a character and the right hand side is an integer. You could make this work if you know that val will only ever be a single digit:

            vals[i] = '0'+val;

However if val could be more than one digit you'll have to use something like sprintf to write the integer into the string. This will probably also require some more complex string allocation and copying characters from original to modified in chunks. In c++ you should probably use std::streambuf

dohashi
  • 1,771
  • 8
  • 12
0

There are a couple problems with the code above.
First, you are declaring arrays of length 0, and no where in the code do you modify modifiedExpr except setting it equal to originalExpr (vals and var are not part of either expression).

Check here for replacing values in strings: How to replace all occurrences of a character in string?

And here for changing ints to ascii: Convert an int to ASCII character

And end up with something like:

modifiedExpr = originalExpr;
char a_val = '0' + val;
std::replace( modifiedExpr.begin(), modifiedExpr.end(), var, a_val);

//Testing the output if it works
std::cout << modifiedExpr << std::endl;
Community
  • 1
  • 1
TheGoatMan7
  • 512
  • 3
  • 5
  • This is almost working. The problem is the last updated expression then return x and not its previous value. Like this. 5 * (2 + 3) - (y + 1); x * (2 + 3) - (6 + 1) – MFJones Aug 11 '14 at 15:11
  • Sure. The issue you are now having is line 1 in my code sets modifiedExpr to originalExpr, which is undoing any changes you may have made. You had the right idea in your original post, which is checking: if(modifiedExpr == "") modifiedExpr = originalExpr; – TheGoatMan7 Aug 11 '14 at 21:13