Reputation: 199
Instead of writing code like this
while (getline(cin, inputWord))
{
if (inputWord.empty() || inputWord == " " || inputWord == " " || inputWord == " ")return 0;
}
How can I get the user to terminate the program regardless of how many whitespaces are present?
Upvotes: 0
Views: 143
Reputation: 17496
Make your own function that counts spaces:
int countWhiteSpaces(string input)
{
// You do it :) Hint: a 'for' loop might do
}
And then use it like this:
while (getline(cin, inputWord))
{
if (inputWord.empty() || countWhiteSpaces(inputWord) > 0)
return 0;
}
Upvotes: 1
Reputation: 50667
Just use
while (getline(cin, inputWord))
{
if(inputWord.find_first_not_of(' ') == std::string::npos) // all spaces
return 0;
}
Upvotes: 1