How to display Greek chars in console ?

This has probably been asked before, but i need to display greek characters on my programs. A cross-platform solution would be best, but i need it to work at least on other windows computers. I use codeblocks with gcc compiler.
When using char to store greek letters i get errors, so i use strings. They work fine when printing a word from input but when printing a fixed message ( see strings c and b below) they print some weird symbols instead. I am still learning the very basics of C++ but if you find a command i can simply use on all my programs even if i dont know what it does it's ok. Some one told me that there is a library that i have to include to use greek characters but he does not remember it's name. I tried using greek encoding (windows-1253) in codeblocks settings with no luck. Here is some example code, notice that the greek alphabet does not print out correctly but the name does.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
using namespace std;
int main ()
{

    string b="αβγδεζηθιξλμνξοπρστυφχψω"; //Greek Alphabet
    string c="abcdefghijklmnopqrstuvwxyz"; //Latin Alphabet
    cout << b << endl <<c << endl;
    cout <<"ΠΩΣ ΣΕ ΛΕΝΕ;" ; // What is your name?
    string name;
    cin >> name;
    cout <<"ΓΕΙΑ ΣΟΥ " << name << endl; // Hello, <name>
}
f9t0 wrote:
A cross-platform solution would be best,

As written it is already cross-platform in the sense that it works on many platforms (sanely configured Linux, BSD, etc), although C++ does not directly require it to work the same way on every platform.

Here's me interacting with your original, unmodified, program on a Linux:
$ ./test
αβγδεζηθιξλμνξοπρστυφχψω
abcdefghijklmnopqrstuvwxyz
ΠΩΣ ΣΕ ΛΕΝΕ;кошка
ΓΕΙΑ ΣΟΥ кошка


To support Windows you will have to say the magic word _O_WTEXT, and then switch all your strings to wide strings to support that, and then load a locale (any Unicode locale) on Linux to support that:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <string>
#ifdef _MSC_VER
#include <io.h>
#include <fcntl.h>
#else
#include <locale>
#include <clocale>
#endif

int main()
{
#ifdef _MSC_VER
        _setmode(_fileno(stdout), _O_WTEXT);
        _setmode(_fileno(stdin), _O_WTEXT);
#else
        std::setlocale(LC_ALL, "en_US.utf8");
        std::locale::global(std::locale("en_US.utf8"));
        std::cout.imbue(std::locale());
        std::cin.imbue(std::locale());
#endif
        std::wstring b = L"αβγδεζηθιξλμνξοπρστυφχψω"; //Greek Alphabet
        std::wstring c = L"abcdefghijklmnopqrstuvwxyz"; //Latin Alphabet
        std::wcout << b << '\n' << c << '\n';
        std::wcout << L"ΠΩΣ ΣΕ ΛΕΝΕ;"; // What is your name?
        std::wstring name;
        std::wcin >> name;
        std::wcout << L"ΓΕΙΑ ΣΟΥ " << name << '\n'; // Hello, <name>
}


on Windows, using Visual Studio 2015, I get this dialog:
αβγδεζηθιξλμνξοπρστυφχψω
abcdefghijklmnopqrstuvwxyz
ΠΩΣ ΣΕ ΛΕΝΕ;кошка
ΓΕΙΑ ΣΟΥ кошка
Press any key to continue . . .


on Linux, I get this:
$ ./test
αβγδεζηθιξλμνξοπρστυφχψω
abcdefghijklmnopqrstuvwxyz
ΠΩΣ ΣΕ ΛΕΝΕ;кошка
ΓΕΙΑ ΣΟΥ кошка


coder777 wrote:
You can get the code page from the console:

Let's leave codepages in 1980s where they belong.
Last edited on
@coder777
that function gave me 18509994788061168 and i have no idea what to do.
also i tried
 
BOOL WINAPI SetConsoleOutputCP(1253);

but it did not help (1253 is greek codepage for windows and 737 for dos)
however i found another command:
 
system ("chcp 1253");

that works, IF i change the font of the command line from raster to lucida console. That would be enough for me, if only i knew how to change the font as the programmer, not have the user change it.

@Cubbi
i get the following on lines 22, 25 and 28 (the lines with greek on the wstring)
error: converting to execution character set: Illegal byte sequence|

which is strange, because normal strings give no error, while wstrings do. By the way, i do not understand the code above line 22. Too advanced for me.
When compiling your code using the "Edit & Run" i get no do not get converting execution error but it shows the following:
terminate called after throwing an instance of 'std::runtime_error'
what(): locale::facet::_S_create_c_locale name not valid

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

Process returned 3 (0x3) execution time : 0.078 s
Press any key to continue.

Last edited on
Sounds like you're not using Visual Studio (which is what my #ifdef _MSC_VER was pivoting on).. I don't know enough about Windows development to comment on other toolchains.
Last edited on
I just tried running my program with
1
2
 
system ("chcp 1253");

but instead of greek, i typed in other Cyrillic (the name you tried) and i got gibberish again. This means that i have to be changing codepages all the time if i want multilanguage support (not practical when coding). And since it works on linux, it's the windows' fault. Is there anyway i can force the program to use unicode?

EDIT: i even tried a simple batch script on windows to output greek alphabet with same result there is something really wrong with them.
Last edited on
Topic archived. No new replies allowed.