Hi, i'm new to C++ (well actually i just haven't looked at it in awhile) and I've been trying to figure out how to make my array's content depend on a condition. It's not extremely important, but I think there's got to be an easier way than what I'm using. I would like to know the best way to fully assign an array with one of many possible sets of data, depending on a condition/s. I've realized (atleast i think, correct me if i'm wrong) that the only way to store lots of values to an array without storing the elements individually or copying a different array to it (but that would be element by element too, just with a simple loop) is when you first initialize it, and nowhere else. Problem is, if i use conditions and declare/initialize the array to different possible things it has a compile error, saying that the array is undeclared when i first access it. If i dont have an unconditional declaration of the array somewhere, it won't even compile. But, the array can only be initialized when it is declared (i think, again help me out here) so i can't have more than one potential initialization. I can't declare an array and pass the address to a function (which function depending on a condition) because the array would have to already be declared to pass it, which means my oppurtunity to initialize it is gone. I can daclare/initialize different arrays unconditionally and then use conditions to copy the appropriate array to the true array, or use a two-dimensional matrix array thingy and only access a certain column (or row or whatever) in that array through the program, which column depending on said condition. There's GOT to be a simple way to do this that I'm not aware of, but HOW?! I would think there is cause It's such a simple thing, to be able to assign many values at once to an array. I know this is a long description for such a simple thing, but I also know it can be annoying when people aren't specific.

Recommended Answers

All 6 Replies

What are these conditions? Can these conditions change while the program is running, in other words depending on user input? or just when the program is compiled? Can you give a code example? I don't think that you can change it at run-time. To change it at run-time, you should use a method like this

if ( condition 1 )
{
       Array = { initializer list 1};
       Do the processing;
       ....
}
if (condition 2)
{
        Array = { initializer list 2};
       Do the processing; // Duplicate of what the above block does. Better use a function for this part. 
       ....
}
.....

Because same code is in two places, the executable gets bulkier. If you use a function for the processing on the array, it will reduce to something like this. And the executable file size will not be significantly smaller.

if ( condition 1 )
{
       Array = { initializer list 1};
       Process_Array( Array );
}
if (condition 2)
{
        Array = { initializer list 2};
       Process_Array( Array );
}
.....

Something like this is not allowed, as you have already found out.

if ( condition 1 )
{
       Array = { initializer list 1};
}
if ( condition 2 )
 {
       Array = { initializer list 2};
}
  Do the processing;
 ....

If you only want it to change during compile time, you can use preprocessor switches.

#define CONDITION_1 1
#define CONDITION_2 0
#if  CONDITION_1 
       Array = { initializer list 1}; // This block will compile
#elif CONDITION_2
        Array = { initializer list 2};
#endif  
 Do the processing;
 ....
#define CONDITION_1 0
#define CONDITION_2 1
#if  CONDITION_1 
        Array = { initializer list 1}; 
#elif CONDITION_2
        Array = { initializer list 2}; // this block will compiler
#endif  
 Do the processing;
 ....

Say I have an array, and I want the content (which list it's iniliazed to) to depend on, say user input, like you said, and like your examples. There's no need for the conditions to change, say they are just numbers from user input. And then the array could be accessed freely throughout the program, having the value list that was picked by the user.

I think i understand now thanks, the access of the array has to be in the same block it was initialized in? A scope thing? Is that why it won't work (says undeclared) if its accessed after the if-condition block that declared it?

I think i understand now thanks, the access of the array has to be in the same block it was initialized in? A scope thing? Is that why it won't work (says undeclared) if its accessed after the if-condition block that declared it?

Yeah. You got it. It is a scope thing.

So I guess there isn't a simple way? It would have made C++ a bit easier I think. :-| Well, I can't make it global or anything, cause it has to be in a block that tests the condition first, which is the cause of the scope problem anyway. Thanks again, you helped me understand. I guess I'll just keep doing it the way i was if i need it to be local everywhere in main(). After all i guess there's nothing wrong with copying the array like i said first, I'll use your method whenever the array doesn't need to be accessed everywhere. I bet scope gives alot of newbs trouble.

You might be able to use a pointer instead. E.g.:

#include <iostream>
 
const int ARRAY_SIZE = 5;
int array1[] = {1,2,3,4,5};
int array2[] = {6,7,8,9,10};
int* arrays[] = { array1, array2 };
 
int main()
{
int idx;
std::cout<<"Enter index: ";
std::cin>>idx;
for (int i=0;i<ARRAY_SIZE;i++)
std::cout<<arrays[idx][i];
 
}

Note that I didn't do any bounds checking, and you'd have to handle things differently if your arrays have different sizes.

commented: Nice. - Wolfpack +3

Yep. That is a nice trick.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.