1

I'm validating string input in C++, to make sure that the input is not empty. I know how to do it using the empty() function with getline(), but I want to know why it's not working when I use cin. When I use empty() with cin, I press enter to try and skip the input, but the empty() never returns true, so I never get re-prompted to enter the string. Is this because the '\n' left in the buffer from cin is counted? I would greatly appreciate the explanation for this.

string name; cout << "Enter your name: "; cin >> name; while (name.empty()) { cerr << "Name can't be empty" << '\n'; cout << "Enter your name: "; cin >> name; } 
3
  • If you look at name.size() the answer should be obvious. Commented Apr 30, 2024 at 19:19
  • 1
    " I want to know why it's not working when I use cin" - You use std::cin in both cases. In one case it's formatted input (skipping whitespaces) and in the other, it's not. Commented Apr 30, 2024 at 19:34
  • Side note: Remember that >> also STOPS reading on whitespace. If the name entered is "John Smith", cin >> name will read "John" into name and leave " Smith" (note the space!) to be found by a future read. To get "John Smith" you would need cin >> first_name >> last_name, but this leads to little booby traps with names like "Victor von Doom". Often you'll find yourself wanting to read names by sucking in an entire name with getline and praying you don't have to think too hard about how to interpret it Commented Apr 30, 2024 at 19:52

1 Answer 1

3

By default, operator>> ignores leading whitespace (you can use std::noskipws) to disable that), and then it stops reading on trailing whitespace. In this case, whitespace includes the Enter key.

So, the operator can never return an empty std::string value on success, only on failure (which your example is not checking for), as the std::string is erase()'ed before the operator attempts to read it:

string name; cout << "Enter your name: "; if (cin >> name) { // success, name.empty() SHOULD always be false here... } else { // fail, name.empty() SHOULD always be true here... } 
Sign up to request clarification or add additional context in comments.

4 Comments

How would I check to see if cin fails?
@Nedmac624 by checking cin's error state after the read. In my example above, the if is handling that, as operator>> will return a reference to cin, and cin has a boolean conversion operator that returns true when the stream is in a good state and false when the stream is in an error state.
That makes sense, but I tried doing "if (cin.fail())", and it didn't evaluate to true when I left the input empty...
Because that is not a failure condition. You can press Enter all you want, those \n keys will simply be ignored as leading whitespace, the read won't exit until you enter 1+ non-whitespace characters followed by whitespace (including Enter). And terminal input is usually line-buffered, so the cin stream usually wont even see the data until Enter is pressed. An actual failure condition would be like pressing Ctrl-C/Ctrl-Z in the terminal, for instance.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.