take input from user in 2d vector

  2d-vector, c++, cin, overriding

I am trying to take values in a 2d vector called alloc from the user but the alloc zero index is the only changed index and the values of each index override it i don’t know why (i.e: only the first index of the 2d vector is affected)

Code:

cout<<"Please Enter n"<<endl;
cin>>n;
cout<<"Please Enter m"<<endl;
cin>>m;


std::vector<vector<int>> alloc ;
int val;
cout<<"Please Enter values of allocation matrix"<<endl;
for(int i=0;i<n;i++){
vector <int> temp;
for(int j=0;j<m;j++){

cin>>val;
temp.push_back(val);
}
alloc.push_back(temp);
temp.clear();
}

Source: Windows Questions C++

LEAVE A COMMENT