cin.getline() is skipping an input in C++
If I use the following code, getline doesn't take the last input(for last iteration of "for" loop, it simply skips it) - int main() { int n; map<string, set<string> > lst; string c,s,c2; cin>>n; for(int i=0;i<n;i++) { getline(cin,c); // here it skips the input for last iteration stringstream ss; ss<<c; bool f=1; while(ss>>s) { if(f) { c2=s; f=0; } else lst[c2].insert(s); } } for (map<string, set<string> >::const_iterator ci = lst.begin(); ci != lst.end(); ++ci) { cout<< (*ci).first <<" "<< (*ci).second.size() <<endl; } } To get rid of it, I put cin.ignore() after getline. Now its taking all