I am preparing for a programming competition soon and for one of the sample problems one line of input is the length of the list of numbers and the second line is the actual numbers. this is what i have:

void main()
{
    int num;
    cin >> num;
    int items[num];
}

it wont compile because it says that num is not a constant expression. I tried this:

int numConst=num

and use numConst as the array index but it still says it wont work. Any help?

Array sizes must be constant in C++. If you want to get the size from input, you need to use a container class (such as std::vector), or simulate an array using pointers and dynamic memory.

okay thanks. is there any way i can resize the array after it has been declared? Like declare it with index of 1 and later on re-size it?

>Like declare it with index of 1 and later on re-size it?
Not portably.

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.