Hi everyone. I am a beginner at C++ and am having some problems with the language. I would appreciate any advice that could be given on this issue.

I cannot figure out how to modify variables! Take this for example.

Say you have four variables, 1A, 1B, 1C, and 1D. You start off with 1A, however would eventually like to transform 1A into 1B. Say you have this equation: 1A + 2 = Answer

In time, you no longer would like to use 1A, but need to use 1B instead of 1A. How would you go about replacing the letter on the variable (A) and putting a different one (B) there in its place?

You could just write another equation to solve for this problem, but what if your code is a mutating type and you cannot predict the need for such modifications without running it first, but to run it, you need to accommodate such modifications? or what if the portion you would like to modify can be 100000 different combinations; surely it is far too tedious to write 100000 derivations of the code.

I mainly want to know how to do this so that I can have a program evolve its own variables and therefore change how it operates over a period of time.


Well if you have different variables you can do this

int a,b,c;
a = 1;
b = 2;
c = 3;
a = b + c;
b = a; // b is now a which is 5

Hmmm....indeed I thought of the same thing a few minutes ago; thanks for verifying that I am not insane ; )

So, is that the only way? Something I tried to do (but failed at) a while ago, was to convert an expression like "A + 2 = B" into a string, manipulate the equation while in string format, and convert back into an int type. It did not work though.

I can see using what you posted to a fair extent...but it has its limits.


You might find the new operator useful :

This is another example :

int a, b, c;

a = 1;
b = 2;
c = 3;

a = b + c;

int *d = new int;

d* = a + b;

well if you have an equation and want to use it for different variables than you could put it into a function.

int Equation(int a, int b)
    int c = a * b + b - 5;
    return c;

// then in you code instead of writing out your equation every time you need if for different variables do this
int a = 2;
int b = 5;
int c = Equation(a,b);
int d = Equation(c,b);
// and so on

Then you can have cases where you 2 arrays and each corresponding element in the arrays needs to be processed. Using the same function as before you could do this.

int a[100], b[100], c[100];
// fill a and b with data
for (int i = 0; i < 100; i++)
    c[i] = Equation(a[i], b[i]);
// now the array named c has all of the results.

Thanks for the advice. I will see what I can do now.