How can I use cin.get() to detect an empty user in

2019-08-22 11:16发布

I've written some code to detect empty user input:

if(cin.get() == '\n')
{
   cout<<"ENTER WAS PRESSED"<<endl;
}

But for some reason if the user presses ENTER it just keeps advancing to the next line as I press ENTER.

标签: c++ input
2条回答
家丑人穷心不美
2楼-- · 2019-08-22 11:45

I think this code does your job.

#include <iostream>
#include <sstream>

using std::cin;
using std::cout;
using std::string;

int main()
{
    string in = "";
    int n = 0;
    while (true)
    {
        cout << "Please enter a number: ";
        getline(cin, in);
        std::stringstream s(in);
        if (s >> n)
            break;
        cout << "Invalid number, please try again\n";
     }
     return 0;
}

AFAIK, I think that you shouldn't use the get() function to input numbers. The get() function is designed to input characters and it behaves as an Unformatted Input Function. The >> operator seems better for this job. The extraction operator >> will wait for your input if you press the ENTER key because it will ignore it. However, the newline input is stored in the cin input stream. You can also try a combination of the peek() and the seekg() function to check for an existing newline input in the input stream and do what you want.

查看更多
Anthone
3楼-- · 2019-08-22 11:57

In this form your code works for me:

#include <iostream>

int main() {
    while (true) {
        if (std::cin.get() == '\n') {
            std::cout<<"ENTER WAS PRESSED"<<std::endl;
        }
    }
    return 0;
}

However, your further comments reveal that your code also includes this:

    while(!(cin >> number) || (number < 0 || number > 3) || (cin.get() == '\n'))

There is your trouble.

What you are trying will not work with that particular while, but many first-term students of C++ ask similar questions, so let me explain. The reason that it will not work is that your concept of std::cin is vague. (By the way, std::cin, please, or cin preceded by using std::cin; if you must. The textbook that is telling you to issue using namespace std; should be burned.)

The object std::cin is a stream—from your perspective, a queue from which your program can accept available input characters when it wishes to do so. Important: std::cin is not a command or an operation, but an object called a stream. By analogy with speech, you may be thinking of std::cin as a verb, only it isn't: it is a noun. By itself, std::cin does not do anything.

What does do something is the extraction operator >>. What also does something is the single-character fetching member function .get(). Both of these retrieve previously unseen characters from the stream std::cin. So, the >> retrieves some characters which the program puts into number, then tests. Then the .get() retrieves another, previously unseen character, which the program checks to see whether it is a newline. But why does your program treat the former group of characters as a possible number and the latter, single character as a possible newline? How does it know that the former group of characters does not begin with a newline?

My suggestion is to try some simpler test programs for a while, using either >> or .get(), but not both in the same program. For now, using both at once is understandably confusing you. Stream input can be tricky. You are unlikely to benefit from further explanation until you get this point straight in your mind, and that will probably take you an hour of trial and experiment. Good luck.

查看更多
登录 后发表回答