How to take input directly without specifying how many of them?

I know the code for this kind of output(It is in the documentation)

How many numbers would you like to type? 5
Enter number : 75
Enter number : 436
Enter number : 1067
Enter number : 8
Enter number : 32
You have entered: 75, 436, 1067, 8, 32

My question is how to take the input numbers and store them in dynamic array with out specifying how many of them? ie., excluding the first step in the output above.
I don't need code i need the idea. Thank you for your time.
Two ways, off the top of my head.

1. Use a vector.
2. create an array with new.
int j;
int *i = new int[j];
cout<<"enter your list of elements"<<endl;
for(int x=0;x<j;x++)

Ok i did this. Its crashing says j is being used without initialization.
I'm unable to figure out what is wrong.Thank you.
int j;
j has no value.

Well, it does have a value, just a garbage one.

I would suggest using vector over new though.
Also, when you new variables, you should delete them too. To stop memory leaks. (:
Last edited on
The simplest way would be to use a vector<int>;
You create a loop and get the number inside a temporary int. you push back the int inside your vector<int> . You also need to do a check for bad input and for what value sould be the exit value.
Got it. Thank you for inputs.
Topic archived. No new replies allowed.