### can`t store different values into vector by user input

Hi guys, i`m facing a problem. I can`t seem to store multiple values into my vector when a user keys in an input.

Lets say a user inputs 1,2 as the first set and later keys in 3,4.
My vector gets overridden by 3 and 4.

i`m trying to pass these values into a function to store them into a vector and use them in the function Hope you guys can enlighten me on this. Thanks in advance

below is my code snippet

 ``12345678910111213141516`` ``````int reconstructSecret(int x, int y) { int getX,getY; getX=x; getY=y; vector myXCordVec; vector myYCordVec; myXCordVec.push_back(getX); myYCordVec.push_back(getY); for(int i=0;i

main method
 ``123456789101112131415`` `````` cout<<"Reconstruction of Secret"<>constructSecret; for(int i=0;i>x; cout<<"please key in the "<>y; reconstructSecret(x,y); }``````
The vectors that you create inside reconstructSecret are local to that function. They will be destroyed when the function ends and the next time the function is called you'll get two different vectors.
i see, so is there any way that i can store the data into a vector in the main method and pass the data inside it to the function?
If you create the vectors in the main function and pass the vectors by reference to the function. That will allow you to use and modify the vectors from within the function.

 ``1234567891011121314`` ``````int reconstructSecret(int x, int y, vector& myXCordVec, vector& myYCordVec) { int getX,getY; getX=x; getY=y; myXCordVec.push_back(getX); myYCordVec.push_back(getY); for(int i=0;i

 ``123456789101112131415`` `````` cout<<"Reconstruction of Secret"<>constructSecret; vector myXCordVec; vector myYCordVec; for(int i=0;i>x; cout<<"please key in the "<>y; reconstructSecret(x,y, myXCordVec, myYCordVec); }``````
Last edited on
thanks man! correct me if i`m wrong, the same theory applies for arrays as well?
Yes but note that if you pass an array to a function
 ``12`` ``````int arr[10]; foo(arr);``````
with the parameter type int*
 ``123`` ``````int foo(int arr[]); // or int foo(int* arr);``````
you are actually passing a pointer to the first element in the array so you don't need to use references here to be able to modify the array elements from within the function.
ok noted on that point! thanks for your help! =)
Topic archived. No new replies allowed.