string to uppercase

ok..this works:
 
  std::transform(st.begin(), st.end(), st.begin(), ::toupper);


but why doesn't this work?
1
2
  for (auto it = st.begin(); it != st.end(); it++)
            ::toupper(*it);
???

Probably it is obvious but I can't see why...
1
2
    for (auto it = st.begin(); it != st.end(); it++)
            *it = ::toupper(*it)
> why doesn't this work?

Because the result of ::toupper(*it) is unused; it is just discarded.

Try: for (auto it = st.begin(); it != st.end(); it++) *it = ::toupper(*it);

Or, better: for( char& c : str ) c = ::toupper(c) ;

Best: for( char& c : str ) c = std::toupper(c) ; // #include <cctype>

Range based loop: http://www.stroustrup.com/C++11FAQ.html#for

<ctype.h> which declares ::toupper() is deprecated.
<ctype.h> (deprecated) behaves as if each name from <cctype> is placed in global namespace http://en.cppreference.com/w/cpp/header
Thanks a lot JLBorges and Yanson. I am enjoying learning C++ and getting help so promptly and clearly makes it even nicer.
Last edited on
Topic archived. No new replies allowed.