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().
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:
//Note: <iostream> and <string> have been included. using namespace std is used
const string hexdigits = "0123456789ABCDEF";
cout << "Enter a series of numbers between 0 and 15"
<< " separated by spaces. Hit ENTER when finished: "
<< endl;
string result;
string::size_type n;
while(cin >> n)
if (n < hexdigits.size())
result += hexdigits[n];
cout << "Your hex number is: " << result << endl;
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?).
I'm here for two reasons:
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
If I can't receive reason 1 without compromising reason 2, I would much rather have reason 1 be fulfilled.
Quick-Edit: I apologize, I'm using Visual Studio 2012