What is the purpose of locales(the class). I managed to stumble upon this while I was looking for help on google. Couldn't get a straight forward definition on what this is? Let me put this into a context if it will help:
This Clause describes components that C++ programs may use to encapsulate (and therefore be more portable when confronting) cultural differences. The locale facility includes internationalization support for character classification and string collation, numeric, monetary, and date/time formatting and parsing, and message retrieval.
locales perform conversions between strings and other data types (numbers, times, monetary units), conversions between character encodings (utf8, ucs4, gb18030, what have you), provide translations of messages into the user's language, classify and convert characters, etc. For the most part. they are used behind the scene by the IO streams library (cout << n; calls a locale facet to format a number as a string). It's a book-length topic
In this case, the author is using the current global C++ locale to convert characters to upper case. This is pointless because main() never changed the global, so it is no different from the default.
Thanks for the quick responses @vlad from moscow and @Cubbi. I have a better idea of what locales does now.
One more question: why is the following code legal?
convert += (toupper(sentence[i]));
the sentence variable was of type string and was not an array. How can the compiler allow this (sentence[i]) , while the user didn't define it as an array in the first place.
The string class overloads the subscript operator to allow you to treat it as if it were an array. Its implementation is probably similar to:
1 2 3 4
char& std::string::operator[](size_t index){
if(index >= this->size()) throw std::out_of_range();
return m_c_string[index]; //<-- Here I assume the actual string is a pointer to a C-string
}