Verify string format with given pattern
I'd instead use the std::isalpha() to check for the alphabets, and for the rest; use isalnum() plus an or condition for the _ character.

Another check, I'd put is whether the input's length is indeed \$ \gt 0 \$. The code then becomes:

// Check correctness, return corresponding bool.
bool ok_tret(const string&amp; nom) {
    if (nom.length() &lt; 1) {
        // handle null strings
        return false;
    }
    if (not (bool) std::isalpha(nom[0])) {
        cout &lt;&lt; err4 &lt;&lt; endl;
        return false;
    } 
    for (unsigned int i = 1; i &lt; nom.length(); ++i) {
        if (not ((bool) std::isalnum(nom[i]) or nom[i] == '_')) {
                cout &lt;&lt; err4 &lt;&lt; endl;
                return false;
        }
    }
    return true;
}