I would like to know is possible to create arrays as user requires. For example
- I ask the user “Do you want coffee”
2.if the user say yes and i create a array of coffee object.
….. - I ask the user “Do you want to have another coffee”?
- if user say yes than i create another array of coffee class
if not i dont create.
Is this achieveable or must i create a fixed number of array?
You cannot create fixed size array at runtime in C++, except some compilers (like g++) provides extension for VLA.
Use
std::vectorinstead. It grows as per your control and automatically deallocates itself when requirement is done.Edit: As the
std::vectorcannot be used by the asker, following is the way usingnew[]with ‘some’ pseudo code:Here
nandsizeare variables(can or cannot be constants) as per your needLater when you are done, deallocate all the memory as
delete[] pQuestions[i];anddelete[] pQuestions;.