2

I have really hard time to get this. If my input is empty, I mean "Enter button" (means empty string, whitespaces), how can I detect it using C++?.

#include <iostream>
#include <stack>
#include <map>
#include <string>
using namespace std;

int main()
{    
    string a;
    cin>>a;

    if(a.empty())
        cout<<"I";
    else
        cout<<"V";

    return 0;    
}

How can I print "I" if it is an empty string and how does it work?
Thanks in advance.

3
  • If the input succeeds with the code as is, a won't be empty. However, input may fail. You should always verify inputs were successful, though. Commented Aug 11, 2016 at 16:24
  • a.empty() returns true if a is 'really' empty (e.g. "" and nothing more, no whitespace or anything). Commented Aug 11, 2016 at 16:26
  • 2
    I somewhat suspect an XY problem. Checking to see if a is "empty" is not the proper way to see if it was read at all. you should be validating the stream state; not the string state. if (std::cin >> a)... Or perhaps you're trying to pull a line from std::cin and determine if it is empty after ripping whitespace? It is a little more complicated in that case. Commented Aug 11, 2016 at 16:30

3 Answers 3

4

If I have understood you correctly:

  1. If you want to check if provided string is empty, just use isspace() function. It checks whether the string is null, empty or white space.

  2. If you want to accept empty input, just use getline() function.

    std::string inputValue;
    std::getline (std::cin,name);
    cout << name;
    

    Function getline() accepts empty input by default.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank u,i have to use getline() func. @mateudu
2

If you want an empty input to be accepted you have to tell the input stream to not skip whitespace using the noskipws IO manipulator:

cin >> noskipws >> a;

Your code should then work as you want it to.

Comments

2

The formatted input functions, i.e., the operator>>() conventionally start off skipping whitespace. The input operator for std::string certainly does that. This is probably not what you want.

You can disable this behavior using the manipulator std::noskipws, e.g., using

if (std::cin >> std::noskipws >> a) {
     std::cout << (a.empty()? "a is empty": "a is non-empty") << '\n';
}

However, the whitespace will be left in the stream. You'll probably want to ignore() it. From the sounds of it, you really want to read a line of input and do something special if it only contains spaces:

if (std::getline(std::cin, a)) {
    std::cout << (std::any_of(a.begin(), a.end(),
                              [](unsigned char c){ return !std::isspace(c); })
                  ? "a contains non-space characters"
                  : "a contains only spaces") << '\n';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.