I have to write a program that reads a string of numbers from user input until it reads 0. For example if the values introduced are 1, 2, 3, 0 then the array X will contain 1, 2 and 3 and the array size will be 3. There is no specified limit for the number of values inserted and I can't just declare an array of any size so is there a way to dynamically increase the size of the array to be able to hold more int values as they are being read?
Asked
Active
Viewed 3.0k times
1
-
4Just curious: why are people downvoting this question? It's not the most detailed or specific, but I wouldn't say I see any huge problems with it. – jtbandes Jun 12 '16 at 18:22
-
1@jtbandes Probably most people want to see ar least some research efforts (if not attempts in code) done from OPs side. There is plenty of information available on Stack Overflow and google regarding this topic. – πάντα ῥεῖ Jun 12 '16 at 18:26
-
When I google "C++ variable length array", the first several results are about VLAs (naturally) which isn't really what the OP wants. I could understand it might be a little difficult to figure this out *if you don't know what you are looking for*. – jtbandes Jun 12 '16 at 18:27
-
1@jtbandes it looks like a homework, also OP has not put any effort into trying to solve this on his own. – marcinj Jun 12 '16 at 18:28
-
1@jtbandes Well googling [c++ dynamic size array](https://www.google.de/search?biw=1600&bih=799&noj=1&q=c%2B%2B+dynamic+size+array&oq=c%2B%2B+dynamic+size+array&gs_l=serp.3..0j0i22i30l9.71538.75682.0.76904.8.8.0.0.0.0.132.950.0j8.8.0....0...1c.1.64.serp..0.5.625...0i8i7i30j0i22i10i30j0i19j0i22i30i19.HBGBQC-DacE) seems to give better results. 3rd result finds the [duplicate for this question](http://stackoverflow.com/questions/13431567/how-to-make-an-array-with-a-dynamic-size-general-usage-of-dynamic-arrays-maybe) – πάντα ῥεῖ Jun 12 '16 at 18:31
-
1Or just make up an antonym. You want an array that isn't "fixed size"... "[c++] array unfixed size" gets results. – Ben Voigt Jun 12 '16 at 18:32
-
1I did do some research on this subject before posting this and did some trials with the information that I found without much success. This is my initial program. `{ int n,x=1,i=1; int *X; while(x!=0) { cin>>x; if(palindrom(x) && x) { X=new int[x]; i++; } } }` – Andrei Ardelean Jun 12 '16 at 18:38
-
This question had been answered already https://stackoverflow.com/questions/4623676/c-variable-array-in-a-class-problem – Pavan Nath Jun 08 '19 at 06:15
3 Answers
13
You can use a std::vector
, which is a variable-length collection to which you can add elements. It manages reallocation automatically when needed. For example:
std::vector<int> values;
values.push_back(0);
values.push_back(1);
...

jtbandes
- 115,675
- 35
- 233
- 266
-
So after it reads the first number I could use values.push_back(1); having the array size increased, is that right? – Andrei Ardelean Jun 12 '16 at 18:22
-
You can use `push_back` to insert values, and you don't have to worry about the size of the array at all. It will automatically allocate memory as needed to hold the elements you've inserted. – jtbandes Jun 12 '16 at 18:23
7
It's called Dynamic Memory Allocation check another question here
But normally i'd use a vector
#include <vector>
using namespace std;
int main(){
vector<int> v;
int x;
while(cin>>x && x != 0){
v.push_back(x);
}
}
You should note that vector
class only exists by implementing dynamic memory allocation. It's just there for you and you don't need to do all the hard work like reserving and destroying the allocated memory...