String Validation function- need explaination

Below is a function from learncpp.com chapter 13.5, compiles and works fine. I cant understand how or why it works. My confusion comes with the switch statement case labels.

There are no (? _ @) symbols in our (###) ###-#### template so how came we expect our template’s indexes will match with those symbols?

These are the case labels:


case # will match any digit in the user input.
case @ will match any alphabetic character in the user input.
case _ will match any whitespace.
case ? will match anything.
default the characters in the user input and the template must match exactly.




Here is the entire code

bool InputMatches(string strUserInput, string strTemplate)
{
if (strTemplate.length() != strUserInput.length())
return false;

// Step through the user input to see if it matches
for (unsigned int nIndex=0; nIndex < strTemplate.length(); nIndex++)
{
switch (strTemplate[nIndex])
{
case '#': // match a digit
if (!isdigit(strUserInput[nIndex]))
return false;
break;
case '_': // match a whitespace
if (!isspace(strUserInput[nIndex]))
return false;
break;
case '@': // match a letter
if (!isalpha(strUserInput[nIndex]))
return false;
break;
case '?': // match anything
break;
default: // match the exact character
if (strUserInput[nIndex] != strTemplate[nIndex])
return false;
}
}

return true;
}

int main()
{
string strValue;

while (1)
{
cout << "Enter a phone number (###) ###-####: ";
getline(cin, strValue); // get the entire line, including spaces
if (InputMatches(strValue, "(###) ###-####"))
break;
}

cout << "You entered: " << strValue << endl;
}

/code]
Those other symbols will not be matched. Only the '#' case and default case will be executed. Im guessing he put the other symbols just to show the range of possibilities. But in this specific case, the are simply not being used.
Topic archived. No new replies allowed.