The problem is that I am trying to print some characters outside the ASCII like the German umlaut characters, the 'ß' and the like. These characters don’t fit in the normal char variable, so obviously I tried to put them in a wchar_t and initialized the string with a L".....". But every time this string contains a character from above, I get the mentioned error, but it is fine with all other characters of the ASCII. This also happens with u"....", U"......" and if I use the u8"....." it doesn’t generate the error but prints garbage.
So to the questions:
Example as requested:
#include <iostream>
#include <string>
using namespace std;
int main()
{
wstring x = L"öäüß" ;
wcout << x ;
return 0 ;
}
This prints the error.
#include <iostream>
#include <string>
#include <locale>
using namespace std;
int main(void)
{
setlocale(LC_ALL,""); // sets locale to german on my computer
string x = "äöüß" ;
cout << x ;
return 0 ;
}
This works properly. But even after changing the locale if I make the first code the error is generated.
Note: I noticed in C++ (not C) that even without including any file else than iostream that all the contents of other files like string or locale or anything else from the standard library is declared and in std namespace. I ignored this for a while and still include the files for documentation but why is this happening. (I am using tdm-gcc 64bit)
I've faced this problem too when compiling multi-platform projects initially edited in Visual Studio. VS uses a different character set (CP1252, at least in my case), while GCC expects UTF-8. You can use the -finput-charset
g++ flag to indicate the encoding of your source code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With