I am trying to set up a program that will allow the user to be able input the size of sn object and then build an array from the inputted size. So basically have

cin << size
-then-
int positioan

I am getting an error about having the array be size 0. What am I doing wrong or is there another way to do this?

Array sizes in C++ must be compile-time constants. In other words, you can't do that. However, you can fake it using pointers and dynamic allocation:

cin>> size;

int *array = new int[size];

But don't forget to free the memory.

Thank you Narue!!!

I sorted through a lot of stack overflow and hadn't found an answer so concise and understandable :)

wwjj - the problem with referring to (and responding to) old threads is that the answer that once was right may not still be so.

Recent changes to the C and C++ standards allow for variables as array size declarators.

Depending on the compiler you use (GCC does suppor this), the following could work

int size;
cin >> size;
int arr[size];

In the interest of portability, you would generally be better off using the point based method Narue gave, or use std::vector

Welcome to the forum. Please be sure to read the stickies at the top, and please don't revive any more necro-threads.

Okay, thank you to you too vmanes! Luckily (or not luckily?) the shared system I code for has an elderly compiler that does not support c++11. And I did indeed use Narue's pointer-based method. Worked great.

As for your requests about necro-thread and stickies: what are stickies, are they the same thing as threads on Daniweb? And what are the site guidelines for when I can and cannot revive a thread, plus other ettiquette/site habits to pick up?

Sorry to ask here-- I did google and poke around the site, but I don't see any intros to newbies for posting guidelines and such. Maybe there's one and I haven't found it, if so could you please point me in the right direction?

Recent changes to the C and C++ standards allow for variables as array size declarators.

C++ still doesn't support non-const array sizes. C has technically supported it since 1999, but compiler support is currently sparse and promises to remain so. Further, C14 has made VLAs optional even for hosted implementations, so they're basically pointless in portable code across the board given that portable code must account for both support and lack of support. And in lack of support, you're right back to the pointer approach. Which means assuming lack of support results in simpler code.

I don't see any intros to newbies for posting guidelines and such.

Whe have basic and obvious rules, but otherwise anything goes. It's not wrong to reply to older threads, though the community tends to frown upon it unless you add something to the discussion.

Sorry for using term "stickies" - that's common on some other forums I inhabit.
Here it's the "read me" threads at the top of the forum section.

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.