0

I'm trying to learn Unicode programming in Windows.

I have this simple program:

#include <iostream> 
#include <string> 

int main()
{
    std::wstring greekWord = L"Ελληνικά";
    std::wcout << greekWord << std::endl;
    return 0;
}

However, it outputs nothing. Any ideas how to make it output Greek?

I tried adding non-Greek letters, and that didn't work quite right either.

ike
  • 11
  • 2
  • It doesn't work like you think it does. Your file is multibyte character encoded, L"Ελληνικά" is not utf-16. – 273K Feb 12 '23 at 06:29
  • What OS and what encoding is the source encoded in? – Mark Tolonen Feb 12 '23 at 06:35
  • 1
    @273K The source file encoding does not have to be UTF-16, but the compiler may need an option to tell it what the source encoding is. For example, the MSVC compiler needs /utf-8 to compile a UTF-8-encoded source file. Windows also needs an OS-specific _setmode() call to support writing to the terminal with wide APIs. See https://stackoverflow.com/a/47501816/235698 – Mark Tolonen Feb 12 '23 at 06:43
  • @Mark. I didn't say the file encoding needs to be utf-16. – 273K Feb 12 '23 at 07:15
  • Sorry, but it still might be utf16, MS Visual Studio tends to switch to utf16 when characters outside the default character set are used. We don’t know what the OP is using as an OS or editor. All we can say is the compiler needs to know the encoding and the terminal may need to be configured. – Mark Tolonen Feb 12 '23 at 07:41
  • @273K The source file encoding can be any one supported by Windows. The compiler just need to be informed about it. – n. m. could be an AI Feb 12 '23 at 09:35

4 Answers4

1

The first thing to try is to make the program not dependent on the encoding of the source file. So use Unicode escapes not literal Unicode letters

std::wstring greekWord = L"\u0395\u03BB\u03BB\u03B7\u03BD\u03B9\u03BA\u03AC"; 

Having the incorrect encoding in the source file is only one thing of many things that could be preventing you from printing Greek. The other obvious issue is the ability of your terminal to print Greek letters. If it can't do that, or needs to be set up correctly so that it can then nothing you do in your program is going to work.

And probably you want to fix the source code encoding issue, so that you can use unescaped literals in your code. But that's dependent on the compiler/IDE you are using.

Jason
  • 36,170
  • 5
  • 26
  • 60
john
  • 85,011
  • 4
  • 57
  • 81
0

If you are outputting your cout to a normal console then the console doesn't usually support unicode text like greek, try setting it up for unicode text or find another way to output your data, like txt files or some gui,

0

There are two way to do this.

The old, non-standard Microsoft way is as follows:

#include <fcntl.h>
#include <io.h>

int main()
{
    _setmode(_fileno(stdout), _O_U16TEXT);
    _setmode(_fileno(stdin), _O_WTEXT);
    // your code here
}

You will fild this everywhere, but this is not necessarily a good way to solve this problem.

The more standards-compliant way is as follows:

#include <locale>

int main()
{    
    std::locale l(""); // or std::locale l("en_US.utf-8");
    std::locale::global(l); // or std::wcout.imbue(l); std::wcin.imbue(l); 
    // your code here
}

This should work with other modern compilers and operating systems too.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
0

TRY this it works with me :

#include

#include <io.h>

#include <fcntl.h>

using namespace std;

int main() {

_setmode(_fileno(stdout),_O_U16TEXT);

wcout<<L"Ελληνικά";

setlocale(LC_ALL,"");

return 0; }