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.
awon't be empty. However, input may fail. You should always verify inputs were successful, though.a.empty()returnstrueifais 'really' empty (e.g.""and nothing more, no whitespace or anything).ais "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 fromstd::cinand determine if it is empty after ripping whitespace? It is a little more complicated in that case.