title: Detecting ENTER key in C++
body: <p>I have a program like this,</p>
  
  <pre><code>char name[100];
  char age[12];
  cout &lt;&lt; "Enter Name: ";
  cin &gt;&gt; name;
  
  cout &lt;&lt; "Enter Age: ";
  cin &gt;&gt; age;
  </code></pre>
  
  <p>Now age is a optional field, so user can simply press ENTER key and have that value as NULL. But the problem is, cin doesn't take ENTER key and just stay there waiting for a valid keyboard char or numerical input.</p>
  
  <p>I think the problem is because cin needs a valid input and thus stays there forever.</p>
  
  <p>So i tried below approach to detect ENTER key press for Age.</p>
  
  <pre><code>cout &lt;&lt; "Enter Age: ";
  if (cin.get() == '\n') {
              cout &lt;&lt; "ENTER WAS PRESSED" &lt;&lt; endl;
          }
  </code></pre>
  
  <p>Now as soon as user presses ENTER key after entering a NAME, it goes straight to the IF condition.</p>
  
  <p>I also tried:</p>
  
  <pre><code>getline(cin, age);
  </code></pre>
  
  <p>But that also simply doesn't stop at <code>cout &lt;&lt; "Age: ";</code> waiting for an input from the user.</p>
  
  <p>The user can either a valid age or simply press ENTER key not to enter a age.</p>
  
  <p>For the below Code: </p>
  
  <pre><code>cout &lt;&lt; "Enter Name: ";
          cin &gt;&gt; name;
  
          cout &lt;&lt; "Enter Age: ";
          if (cin.get() == '\n') {
              cout &lt;&lt; "ENTER WAS PRESSED" &lt;&lt; endl;
          }
  </code></pre>
  
  <p>The Output is:</p>
  
  <pre><code>Enter Name: asdf
  Enter Age: ENTER WAS PRESSED
  </code></pre>
  
  <p>I want the cursor to wait at <strong>Enter Age: _</strong></p>
  
  <p>How can i do that?</p>