unknown array size

Hello i have a problem to solve...
I have to read an undefined sequence of numbers that terminates with the value -1
this has to be stored in an array(not a vector)

Is there any way to store unknown number of elements in an array?

Does the -1 can help me?

Thanks in advance
The size of an array cannot be changed. However, you can create a larger array and copy the old contents over, which is exactly what a vector is doing.
i have done the following so far is working but i am not sure if is the correct thing because i limit the numbers to 100


int ar [100];
int count=0;
int input;
cout<<"Please enter a sequence of positive integers (-1 to finish)"<<endl;
for(int i=0; i<100;i++){

cin>>input;
if(input!=-1){
ar[i]=input;
count++;
}
else{
break;
}
}
int* new_arr = new int[count];
cout<<"Copied array:";
for(int i=0; i<count;i++){
new_arr[i]=ar[i];
cout<<new_arr[i]<<" ";
}
That looks more or less okay, however you need to allocate the original array dynamically as well, so you can delete[] it later. And you only need to allocate a new array whenever the current capacity runs out (if you have more than 100 elements).
Topic archived. No new replies allowed.