title: Cin in a while loop
body: <p>So, I've looked around and haven't been able to figure out what's going on with cin during my While loop. I'm working through the book C++ Primer (5th edition) and I noticed that during one of the exercises I couldn't use cin to grab strings without it not terminating the while loop. I fixed this issue by just using getline().</p>
  
  <p>The goal of the current exercise is to ask for user input from the values of 0 - 15, and converting that number into it's "Hex Equivelant" (Where 0 = 1, 1 = 2, 2 = 3, ... , 10 = A, 11 = B). I tried doing this without the book and failed, but then started questioning the code in the book. Here's the code in the book:</p>
  
  <pre><code>//Note: &lt;iostream&gt; and &lt;string&gt; have been included. using namespace std is used 
  const string hexdigits = "0123456789ABCDEF";
  cout &lt;&lt; "Enter a series of numbers between 0 and 15"
       &lt;&lt; " separated by spaces. Hit ENTER when finished: "
       &lt;&lt; endl;
  string result;
  string::size_type n;
  
  while(cin &gt;&gt; n)
      if (n &lt; hexdigits.size())
          result += hexdigits[n];
  cout &lt;&lt; "Your hex number is: " &lt;&lt; result &lt;&lt; endl;
  </code></pre>
  
  <p>If I were to run this code, it would never terminate the while loop after hitting enter without entering any code (essentially giving the input of whitespace I would think?).</p>
  
  <p>I'm here for two reasons:</p>
  
  <p>1) Why does this code not work?
  2) I would appreciate a nudge in the right direction, but not the answer to how to get this code to execute correctly</p>
  
  <p>If I can't receive reason 1 without compromising reason 2, I would much rather have reason 1 be fulfilled.</p>
  
  <p>Quick-Edit: I apologize, I'm using Visual Studio 2012</p>