c++ - how to add characters that aren't alphanumeric -
i have program working way want handling alphanumeric values wish create exception of allowing periods, hyphens, , underscores. want negate other characters illegal.
void accountdata::assignaccount() { std::cout << "input account name: "; std::string inputaccount; std::getline(std::cin, inputaccount); std::string useaccount = inputaccount.substr(0, 15); if (std::all_of(begin(useaccount), end(useaccount), std::isalnum)) varaccount = useaccount; else { bool valid = true; while (valid) { std::cout << "\naccounts can contain alphanumeric values exceptions of _-.\n\ninput account name: "; std::getline(std::cin, inputaccount); useaccount = inputaccount.substr(0, 15); if (std::all_of(begin(useaccount), end(useaccount), std::isalnum)) { varaccount = useaccount; valid = false; } } } }
you can write own predicate , use all_of
this:
bool myfun(char a) { return (isalnum(a) || a=='_' || a=='-' || a=='.'); } void accountdata::assignaccount() { std::cout << "input account name: "; std::string inputaccount; std::getline(std::cin, inputaccount); std::string useaccount = inputaccount.substr(0, 15); if (std::all_of(begin(useaccount), end(useaccount), myfun)) varaccount = useaccount; else { bool valid = true; while (valid) { std::cout << "\naccounts can contain alphanumeric values exceptions of _-.\n\ninput account name: "; std::getline(std::cin, inputaccount); useaccount = inputaccount.substr(0, 15); if (std::all_of(begin(useaccount), end(useaccount), myfun)) { varaccount = useaccount; valid = false; } } } }
Comments
Post a Comment