C++: how do I check if the cin buffer is empty?

82,931

Solution 1

When reading from std::cin, it's preferable not to use the stream extraction operator >> as this can have all sorts of nasty side effects. For example, if you have this code:

std::string name;
std::cin >> name;

And I enter John Doe, then the line to read from cin will just hold the value John, leaving Doe behind to be read by some future read operation. Similarly, if I were to write:

int myInteger;
std::cin >> myInteger;

And I then type in John Doe, then cin will enter an error state and will refuse to do any future read operations until you explicitly clear its error state and flush the characters that caused the error.

A better way to do user input is to use std::getline to read characters from the keyboard until the user hits enter. For example:

std::string name;
getline(std::cin, name); // getline doesn't need the std:: prefix here because C++ has ADL.

ADL stands for argument-dependent lookup. Now, if I enter John Doe, the value of name will be John Doe and there won't be any data left around in cin. Moreover, this also lets you test if the user just hit enter:

std::string name;
getline(std::cin, name);

if (name.empty()) {
    /* ... nothing entered ... */
}

The drawback of using this approach is that if you want to read in a formatted data line, an int or a double you'll have to parse the representation out of the string. I personally think this is worth it because it gives you a more fine-grained control of what to do if the user enters something invalid and "guards" cin from ever entering a fail state.

I teach a C++ programming course, and have some lecture notes about the streams library that goes into a fair amount of detail about how to read formatted data from cin in a safe way (mostly at the end of the chapter). I'm not sure how useful you'll find this, but in case it's helpful I thought I'd post the link.

Hope this helps!

Solution 2

cin will not continue with the program unless the user enters at least 1 character (enter doesn't count). If the user doesn't give ANY input, cin will just keep waiting for the user to give input and then press enter.

Share:
82,931
Admin
Author by

Admin

Updated on July 15, 2022

Comments

  • Admin
    Admin almost 2 years

    How do you check to see if the user didn't input anything at a cin command and simply pressed enter?

  • JesusChrist
    JesusChrist about 13 years
    I dont have compiler so I cant check. but thanks for telling me.
  • Fred Nurk
    Fred Nurk about 13 years
    Almost true; depends what operation you do with cin.
  • Fred Nurk
    Fred Nurk about 13 years
    Istreams do not have an extractor for a string pointer (your str is an array of strings). You can always use an online compiler to check.
  • Marlon
    Marlon about 13 years
    Also, you are allocating 100 strings. You probably meant char[100].
  • Fred Nurk
    Fred Nurk about 13 years
    @Marlon: Hopefully he meant string str;, but with yours or mine, it still has problems.
  • matzahboy
    matzahboy about 13 years
    @Fred-Nurk - Can you give me an example?
  • Benjamin Lindley
    Benjamin Lindley about 13 years
    Why are you just making stuff up?
  • Fred Nurk
    Fred Nurk about 13 years
    Getline was mentioned in another answer, but cin.get and cin.ignore are others.
  • Jesse Smothermon
    Jesse Smothermon about 13 years
    Thank you for this answer, it really helped me out with a program I'm working on. I'm putting in this comment because I found something interesting. I have an if-else statement that checks if the user inputs "y", "n", something else, or nothing at all. I found that the program won't hit the "nothing at all" case unless I start the if-else with that case. I'm not sure if that also happened to other people.
  • Jamal
    Jamal over 8 years
    Any problem? Lots of problems, actually.