change variable name with a loop

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


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...

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;
}

Tags:

C++