0

I am new to C++ and learning vectors. Want to read input as below and using string vectors.

2
stack
overflow

3
stack
heap
overflow

Below is my code.Here, getting only 1 string as output. Please point out where I am making mistake.

int main() {
vector<string> my_strings;
string buf;
int n;

cin >> n;
for(int i=0;i<n;i++){
    getline(cin,buf);
    my_strings.push_back(buf);
}
for(int i=0;i<n;i++){
    cout <<  my_strings[i] << endl;
}

return 0;
}
Abhi
  • 189
  • 1
  • 8
  • Read the [notes section](http://en.cppreference.com/w/cpp/string/basic_string/getline). – StoryTeller - Unslander Monica Dec 27 '17 at 13:28
  • Did you search before posting? We'll probably find that equivalent questions have been asked many times over. See e.g. https://www.google.co.uk/search?q=stackoverflow+c%2B%2B+getline+only+one+site:stackoverflow.com – underscore_d Dec 27 '17 at 13:28
  • Easy solution, read all user input with getline, including *n* (then use for example stringstream to parse that). – hyde Dec 27 '17 at 13:30
  • Actually want to use vector, particularly string vector to read this type of input and how to do that. I didn't find the same on duplicate question link – Abhi Dec 27 '17 at 13:38

0 Answers0