title: Getline keeps on getting newline character. How can I avoid this?
body: <p>Basically I first takes an integer as input and then test case follows. My each test case is an string. I am suppose to print the string back if the starting patten of string matches "HI A" and it is case-insensitive. I wrote the code below to accomplish to this. My problem is that when I press enter after each input, getline takes newline character as new input. I have tried to tackle this by using extra getline after each input but the issue is still there. Program gets stuck in the loop even though I have put a break condition. What am I doing wrong?</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  using namespace std;
  int main(){
      int N;
      cin &gt;&gt;N;
      string nl;
      getline(cin,nl);
      for (int i=0;i&lt;N;i++){
          string s;
          getline(cin,s);
          //cout &lt;&lt;"string"&lt;&lt;s&lt;&lt;endl;
          int flag=0;
          if ((s.at(0)=='h'||s.at(0)=='H')&amp;&amp;(s.at(1)=='i'||s.at(1)=='I')&amp;&amp;(s.at(2)==' ')&amp;&amp;(s.at(3)=='a'||s.at(3)=='A')) flag=1;
  
          if (flag==1) cout &lt;&lt; s;
          //cout &lt;&lt; "not " &lt;&lt;s;
          string ne;
          cout &lt;&lt; "i="&lt;&lt; i&lt;&lt;endl;
          if (i==N-1) {break;}
          getline(cin,ne);
  
      }
  }
  </code></pre>
  
  <p>Here is sample input:</p>
  
  <pre><code>5
  Hi Alex how are you doing
  hI dave how are you doing
  Good by Alex
  hidden agenda
  Alex greeted Martha by saying Hi Martha
  </code></pre>
  
  <p>Output should be:</p>
  
  <pre><code>Hi Alex how are you doing
  </code></pre>