Note that there are some explanatory texts on larger screens.

plurals
  1. POcin.getline() is skipping an input in C++
    primarykey
    data
    text
    <p>If I use the following code, getline doesn't take the last input(for last iteration of "for" loop, it simply skips it) -</p> <pre><code>int main() { int n; map&lt;string, set&lt;string&gt; &gt; lst; string c,s,c2; cin&gt;&gt;n; for(int i=0;i&lt;n;i++) { getline(cin,c); // here it skips the input for last iteration stringstream ss; ss&lt;&lt;c; bool f=1; while(ss&gt;&gt;s) { if(f) { c2=s; f=0; } else lst[c2].insert(s); } } for (map&lt;string, set&lt;string&gt; &gt;::const_iterator ci = lst.begin(); ci != lst.end(); ++ci) { cout&lt;&lt; (*ci).first &lt;&lt;" "&lt;&lt; (*ci).second.size() &lt;&lt;endl; } } </code></pre> <p>To get rid of it, I put cin.ignore() after getline. Now its taking all the inputs but I'm facing a new issue -</p> <pre><code>#include&lt;iostream&gt; #include&lt;string&gt; #include&lt;map&gt; #include&lt;set&gt; #include&lt;sstream&gt; #include&lt;algorithm&gt; using namespace std; int main() { int n; map&lt;string, set&lt;string&gt; &gt; lst; string c,s,c2; cin&gt;&gt;n; for(int i=0;i&lt;n;i++) { getline(cin,c); cin.ignore(); stringstream ss; ss&lt;&lt;c; bool f=1; while(ss&gt;&gt;s) { if(f) { c2=s; f=0; } else lst[c2].insert(s); } } for (map&lt;string, set&lt;string&gt; &gt;::const_iterator ci = lst.begin(); ci != lst.end(); ++ci) { cout&lt;&lt; (*ci).first &lt;&lt;" "&lt;&lt; (*ci).second.size() &lt;&lt;endl; } } </code></pre> <p>The new issue is that while taking c2, first character of string gets removed. For example, if I give "England Jane Doe" as input to getline, in c2 I'll get "ngland".</p> <p>How to get rid of this issue now?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload