Restoring my locale?

Before my program access the bellow code, the display is:

Country: CANAD¿
Number: 3872

After the code is this:

Country: CANADÁ
Number: 3,872


I need the letter Á to remain, but I also need the number to be displayed without a comma, what do I need to change in my code?
const std::wstring& ABC::strtools::toUpperCase( const std::wstring& _str )
throw(...) {
std::locale::global(std::locale(""));
std::wcout.imbue(std::locale());
auto& f = std::use_facet<std::ctype<wchar_t>>(std::locale());
this->setString(_str);
wapstr.clear();
wapstr.assign(this->getString());
f.toupper(&wapstr[0], &wapstr[0] + wapstr.size());
this->setString(wapstr);
return this->getString();
}
Your code fragment there is very disorganized. For example, you are doing a lot of setting and getting of the string property of your class -- you really shouldn't need to do anything but a single set, right?

In any case, the problem is that you are messing with std::wcout's locale. But you don't need to do that. (So don't.)

Hope this helps.
nice to see someone aware of C++'s well-hidden toupper for arrays.

if you don't want the comma, change num_put back to C:

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <locale>

int main()
{
    std::wstring str = L"Country: CANADÁ\nNumber: ";

    std::ios::sync_with_stdio(false);
    std::wcout.imbue({std::locale(""), std::locale(), std::locale::numeric});
    std::wcout << str << 3872 << '\n';
}


demo: http://coliru.stacked-crooked.com/a/4d27be2c2c29db49 (had to replace "" with a named locale since coliru's environment sets locale to "C")
Last edited on
Topic archived. No new replies allowed.