[SOLVED]taking input into vector using for-range loop

  c++, input, vector

First of all, I want my user to tell me how many numbers he have to input? Which will create that number of elements in vector initialized to zero. Then I want to use
for-range loop to insert the elements into the vector and similarly an other for-range loop to display the vector elements.

#include<iostream>
#include<vector>
using std::cout;
using std::vector;
using std::cin;

int main(){
  int c;
  cin>>c;
  vector<int> ivec(c); 
  
  for(int i : ivec){ //for taking input and adding it into vector 
    cin>>i;
    ivec.push_back(i);
  }

  for(int i: ivec){ //displaying the vector 
    cout<<i<< "t";
    }
 

  return 0;
}

but my output is very different from my expectation.

My actual output is coming as:-

Output :-

3
4
5
6
0 0 0 4 5 6 

can somebody explain me please? And can I actually use a for-range loop for inserting vector elements?

Source: Windows Questions C++

LEAVE A COMMENT