inhaler inhaler - 1 year ago 114
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;


return 0;

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

Thanks in advance.

Answer Source

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.