tags:

views:

855

answers:

6

Is there a way without using arrays to write the following with a loop:

cout<<"This variable c1 ="c1
cout<<"This variable c2 ="c2
cout<<"This variable c3 ="c3

for(i=1,i<8,i++)
cout<<"This variable c%d =",i<<**????**<<

This is obviously not what I Need to be done but is the easiest example I could think of with the same problem... So what I would like to do is change the variables in the loop, not the output!

EDIT: Thanks a lot for all the input, here is a bit more of the code to help illustrate my problem...Im Using Cplex with c++. The loop will not end at seven but when a stop criteria is met

static void  populatebyrow (IloModel model, IloNumVarArray x, IloRangeArray c)
{
    IloExpr c1(env);
    IloExpr c2(env);
    IloExpr c3(env);
    IloExpr c4(env);

    c.add(c1>=n);
    c.add(c2>=n); ...

    model.add(c);
}

I Want to add these expressions to an Array called c that will be an input for a model in cplex. Then after I get a result from Cplex I want to add an expression c(i) and solve it again... This until i get the values I want... IloExprArray could also be used somehow, but then I dont know how to add the expressions using this method:

for(i= 0,...)
{
    c7 +=x[i];
}
+6  A: 
int* varArray[] = {&c1, &c2, &c3};
int size = sizeof( varArray) / sizeof(int*);

for( int i = 0; i < size; ++i)
{
   std::cout << "This variable c"<< i+1 << " = " << *varArray[i] << std::endl;
}
Cătălin Pitiș
Maybe I'm missing something, but why loop up to 7?
Daniel Daranas
My mistake. It should be the length of the array.
Cătălin Pitiș
Changed the code accordingly.
Cătălin Pitiș
+10  A: 

If I understand correctly, you are trying to create variable names dynamically. AFAIK this is not possible with C++.

yhager
I was afraid for that...Thanks anyways
mrbuxley
Well he could put a big switch statement in the loop but it's kind of ridiculous
annakata
+8  A: 

I'd recommend using an array for this. You should not be playing with dynamic variable names in a compiled language.

int c[] = {2, 5, 7, 9, 3, 4, 6, 5};
for (int i = 0; i < 8; i++) cout // and so on...
Blixt
A: 

Well if you really can't go with an array, why not trying with a pointer. I know the following example is essentially an array, however it is slightly different so it may be worth a shot.

#include <iostream>
#include <conio.h>

#define N 10

using namespace std;

int main() {

    int * a = new int[N];

    for (int i = 0, * b = a; i < N; i++, b++) {
        *b = i;    
    }


    for (int i = 0, * b = a; i < N; i++, b++) {
        cout << *b << endl;
    }

    getch();
}

This is still an array, however it is used slightly different and just may be what you are looking for. If it is not, please clarify your question so we may help you. Even if that is the simplest example you can think of, try adding some more detail.

EDIT I just saw (in a comment) you were trying to create variable names dynamically. As far as I know that's not possible, however you may try implementing (or use a library, I'm sure there is plenty out there) a HashMap . Using a hashmap you can link an item to a string (key) so you would be able to link an item to a custom string, entered by the user (assuming that's what you need this for).

Rekreativc
A: 

I Always learned that if "variable variablenames" is the asnwer your asking the wrong question. It can be solved differently.

the_ajp
+1  A: 

If you want to do anything lexically, you're stuck with a macro. Please understand that this is terrible and unmaintainable, but if you insist, you could do something like this:

#define Q(a) a
#define DO_THING(a, expr) do { Q(a)1 expr; Q(a)2 expr; Q(a)3 expr; Q(a)4 expr } while (0)

DO_THING(b, [i] = i);

This results in the code:

do { b1  [i] = i; b2  [i] = i; b3  [i] = i; b4  [i] = i } while (0);

Having written all that out, please please please don't do this. Even if you wrap it in more macros to make it quasi-readable, please please let common sense prevail and abandon this plan.

Dan Olson