My problem here is I don\'t know how to insert a rule wherein if a user inputted a number on the string, it will cout
a warning saying it\'s not valid, same wit
How about something like this:
std::string str;
std::cin >> str;
if (std::find_if(str.begin(), str.end(), std::isdigit) != str.end())
{
std::cout << "No digits allowed in name\n";
}
The above code loops through the whole string, calling std::isdigit for each character. If the std::isdigit
function returns true for any character, meaning it's a digit, then std::find_if returns an iterator to that place in the string where it was found. If no digits were found then the end
iterator is returned. This way we can see if there was any digits in the string or not.
The C++11 standard also introduced new algorithm functions that can be used, but which basically does the above. The one that could be used instead is std::any_of:
if (std::any_of(str.begin(), str.end(), std::isdigit))
{
std::cout << "No digits allowed in name\n";
}