I want to automate the creation and editing of a series of sequential variables within a piece of code, but I'm still fresh to C++, so I'm a little shaky on the doing part.
What I'm looking for is a way to have a pre-defined variable suffix, i.e. "var" and be able to append numbers onto it, automatically i.e. "000" - "999" to create a series of variable labels; without having to type in every single one manually, or having 1000+ lines of code just to declare those variables.
I know about arrays, I was just seeing if there was another way about it. I guess I'm trying to unnecessarily overcomplicate the matter though, now that I reconsider it.
You can't be serious? There is no way to use that. The only reason it would ever compile is if the compiler was smart enough to remove dead code (many are).
The C preprocessor is not capable of replicating names like that. You'd have to use something smarter, like m4.
And even then, the only place in the code where you know for sure that the variable exists is where you define it!
Also, you ought to note that the preprocessor does it's work before runtime, so it will not be running through the for loop checking values of i. Assuming I understand the preprocessor correctly, it will just scroll down through the code once and give you a loop like:
1 2 3
for(int i = 0; i < number_of_vars_u_need;i++) {
int xi = 0;
}
I don't really see the point of all this. What with there being arrays and all...
Also, defining the variables inside the block limits their scope to within the block. And the preprocessor doesn't have the value of 'i' at compile time. You'd just be allocating and deallocating a variable named 'xi' 'number_of_vars_u_need' times.