AboutEddie Expertise I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by ExpertRating.com. I also know a good deal about graphics via OpenGL, and GUIs.
Experience I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.
Question Is there a way to set/change the number of elements within an array at runtime? Everything I've done so far gets an error stating that the number of elements within the array has to be a constant. Thanks in advance for your help!
-Neil
Answer Hello Neil, thank you for the question.
Sure, you can do that. The easiest way to allocate at runtime is with a pointer. Here is a simple example:
int numItems;
cout << "Enter the number: ";
cin >> numItems;
int *array = new int[numItems];
// do something with the array
for(int i = 0; i < numItems; i++)
{
array[i] = i;
}
// clean up
delete [] array;
That should have you pointing in the right direction.