My code
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main(){
int s;
cin>>s;
vector<string> v(s);
for(int i=0;i<s;i++){
getline(cin,v[i]);
}
cout<<v[1]<<endl;
return 0;
}
I want to accept an array of strings where strings have spaces in them. The above program doesn't work as it terminates before the loop ends and instead of the second string in the vector v[1]
it prints first v[0]
no matter what the value of int s
is.
Can anyone explain why this is happening and how can I correctly accept an array of strings with spaces?