I am trying to enter the values in a array by for loop but loop is taking only one value and than program is closed

Viewed 22

I created an array of user-defined size and trying to enter the values but when for-loop is executed it is only taking one value

#include<iostream>
#include<vector>
using namespace std;
int main()
{
    int s;
    cin>>s;
    vector <int> arr;
    int input;
    for(int i=0;i<s;i++)
       {cin>>input;
        arr[i]=input;}
       for(int i=0;i<s;i++)
        cout<<" "<<arr[i];
}

my input

5

1 2

expected input

5

1 2 3 4 5

output

program crashed after entering the value 1

expected output

1 2 3 4 5

0 Answers
Related