title: Using std::getline() to read a single line?
body: <p>My goal is to prompt user to enter a message / sentence and then print it out on the screen, using <code>getline()</code>. The following is two different attempts I have tried out. </p>
  
  <p>First Attempt: </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;iomanip&gt;
  #include &lt;cstring&gt;
  using namespace std;
  int main(){
  
      chat message[80];
      cout &lt;&lt; "\n what is your message today?" &lt;&lt; endl;
  
      cin.getline( message, 80); // Enter a line with a max of 79 characters.
    if( strlen( message) &gt; 0)  // If string length is longer than 0.
      {
  
        for( int i=0; message[i] != '\0'; ++i)
            cout &lt;&lt; message[i] &lt;&lt; ' ';
        cout &lt;&lt; endl;
  
     }
   }
  </code></pre>
  
  <p>Second Attempt:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;iomanip&gt;
  #include &lt;cstring&gt;
  using namespace std;
  int main(){
  
      string a = "a string";
      cout &lt;&lt; "\n what is your message today?" &lt;&lt; endl;
      while(getline(cin,a))
          cout &lt;&lt; a;
      cout&lt;&lt;endl
  
     }
   }
  </code></pre>
  
  <p>For the fist attempt, the code simply print out "what is your message today?" and quit. I do not have a chance to enter any string at all. For the second attempt, it keeps asking me enter the message. Each time, when I enter something with the "\n", it would display what I entered on the screen. I use control + c to interrupt the running process to make it stop.</p>
  
  <p><strong>EDIT:</strong> To clarify and explain on my side, I extract the first attempt from a longer code, which is as the following. </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;iomanip&gt;
  #include &lt;cstring&gt;
  using namespace std;
  
  char header[] = "\n *** C Strings ***\n\n";  // define a c string 
  int main()
  {
    char hello[30] = "Hello ", name[20], message[80];  // define a c string hello, declare two other c strings name and message
    string a="fivelength";
    cout &lt;&lt; header &lt;&lt; "Your first name: ";
    cin &gt;&gt; setw(20) &gt;&gt; name;      // Enter a word.
  
  
    strcat( hello, name);      // Append the name.
    cout &lt;&lt; hello &lt;&lt; endl;
    cin.sync();                // No previous input.
    cout &lt;&lt; "\nWhat is the message for today?"
         &lt;&lt; endl;
  
    cin.getline( message, 80); // Enter a line with a max of 79 characters.
    if( strlen( message) &gt; 0)  // If string length is longer than 0.
      {
  
        for( int i=0; message[i] != '\0'; ++i)
            cout &lt;&lt; message[i] &lt;&lt; ' ';
        cout &lt;&lt; endl;
  
     }
  return 0;
  }
  </code></pre>
  
  <p>For the above code, it does not give me a chance to enter a message on the screen. I will put it as another question. </p>