Search code examples
c++while-loopcin

Cin in a while loop


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


Solution

  • Hitting Enter produces either a CR or LF or both, depending on your platform. This is a valid input so satisfies the condition to continue the while loop. You will need to either explicitly test for these characters at the beginning of your input or use Ctrl-C to break out of the loop.

    As a readability issue, I would include braces around the code that you want in the loop. What you have there is valid C++ since without braces the while will loop on the next statement and the whole if conditional is a single statement. Practice putting them in now even on single line loops and you'll save yourself some headache debugging in the future.