inhaler inhaler - 3 months ago 9
C++ Question

Checking empty string with 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;

}


I have really hard time to get this..if my input is nothing,i mean "Enter button"(means empty string,whitespaces),how to detect it with c++ language.Example is this code...how can i print "I" if it is a empty string & how it works ?
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.

Comments