inhaler inhaler - 3 months ago 12
C++ Question

C++ - Checking if string is empty

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.

Answer

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.