I'm trying to write validation for a string that follow the following guidelines:
Can be alphabet
Can be '.' or ','
Can have a space
CAN'T be blank
So for example, "Bob", "Bob Smith", and "Bob Smith, Jr." should be fine, while " " or just hitting the enter key shouldn't.
If the string follows these guidelines the function should simply return it, but if it doesn't it should continue looping through until a correct entry is entered.
I've tried a few different ways of looping through to validate, and if I'm understanding break correctly, I believe this structure should work. That said, entering a simple string such as "Bob" is failing the tests. I'm also unsure how to make sure the user can't just hit the space bar or press enter.
std::string patronName()
{
std::string name;
bool loopFlag = true;
do
{
std::cout << "Please enter the name of the Patron: ";
std::getline(std::cin, name);
for (int i = 0; i < name.length(); i++)
{
if (!isalpha(name[i]) || !ispunct(name[i]) || !isspace(name[i]) || name.empty())
{
std::cout << "Invalid name entry." << std::endl;
break; //If we're invalid, doesn't matter what the rest is
}
loopFlag = false;
}
}
while(loopFlag);
return name;
}
Are there any obvious errors I'm missing in my logic?