Hello, I just made a function that will turn your string into a lowercase letter but every time I run it doesn't work. The compiler says "tolower ignored return value". Is there a way to fix this?
std::string to_lower(std::string word)
{
for (char& c : word)
c = (char)std::tolower(c);
return word;
}
void to_lower(std::string& word)
{
for (char& c : word)
c = (char)std::tolower(c);
}
> for (char& c : word)
> c = (char)std::tolower(c);
This may engender undefined behaviour.
Like all other functions from <cctype>, the behavior of std::tolower is undefined if the argument's value is neither representable as unsigned char nor equal to EOF. To use these functions safely with plain chars (or signed chars), the argument should first be converted to unsigned char https://en.cppreference.com/w/cpp/string/byte/tolower#Notes
Note that the function that yvez used (std::tolower in <locale>) does not have this particular problem.