0

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?

Edited by mike_2000_17: Fixed formatting

2
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by Narue
0

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.

0

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?

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.