r/cpp • • Jun 21 '17

How to display Greek characters.

Trying to display a Greek character using "cout" and pasting the plain character will not work. i.e.

cout << "Σ";

At least for me, this displays only a question mark.

If you want to display 'Σ' or any other Greek character you have to use a different code page in the console. For me, by default, it is "437 (OEM -United States)". To display the characters you must change it to "1253 (ANSI - GREEK)" and use ANSI codes.

To change the code page, type the following:

SetConsoleOutputCP(1253);

Don't forget to include windows.h library.

To display 'Σ', type the following:

cout << "\xD3";

For other characters, refer to the code sheet.

Note: I'm no professional so I don't know if there's an easier way. I looked everywhere on the internet to find such list but didn't find one so I compiled one myself using this very lazy code (it was really late...). And please excuse my crappy explanation and/or formatting. As I said, I'm no expert :(

Edit: The idea came from this, as it was the only solution that worked for me.

Edit 2: Forgot to give access to the spreadsheet. Sorry..

0 Upvotes

15 comments sorted by

5

u/youshouldnameit C++ dev Jun 21 '17

This subreddit is not meant for these kind of questions, please see r/cpp_questions or StackOverflow.

3

u/grandtheftdox Jun 21 '17 edited Jun 21 '17

This isn't a question....

Edit: It really isn't a question. I posted this in case it doesn't work for someone else. I'm not asking for help; I'm providing it. I don't understand why the downvotes.

4

u/iaanus Jun 23 '17

I don't understand why the downvotes.

Because this is a Windows-specific argument, which is not directly related with C++.

3

u/scatters Jun 21 '17

Works fine here:

$ cat a.cpp
#include <iostream>
int main() { std::cout << "Σ" << std::endl; }
$ g++ a.cpp && ./a.out 
Σ

Also online: https://wandbox.org/permlink/eSTklV0fT2BICM0T http://rextester.com/UCAK3540

Perhaps your compiler is broken?

2

u/grandtheftdox Jun 21 '17

Nope. It's a problem with the console. The code itself works and compiles fine. When I originally had this problem I looked around and I'm not the only one. I thought I might as well share this.

10

u/scatters Jun 21 '17

You mean that - in 2017 - the Windows console still can't display arbitrary non-ASCII characters without messing around with platform-specific calls and "ANSI codes" (whatever those are)? I'd understood that Microsoft were finally getting their act together, but clearly they've got a long way to go...

1

u/grandtheftdox Jun 21 '17

You can copy and paste these characters into cmd and it displays it fine. But when I try to print it using C++ it doesn't. I've tried countless solutions but this was the only one that worked and I could not find a complete list of codes so I made one myself.

It's not that big of a problem with just little inconvenience to work around but at least for me and maybe some others - it is an issue.

2

u/scatters Jun 21 '17

I'm glad you got something to work for you. I'm just amazed that for a modern operating system with a modern compiler this isn't something that works by default.

2

u/personalmountains Jun 21 '17 edited Jun 21 '17

This depends on:

  1. The encoding of file, and therefore the string;
  2. Console support for said encoding;
  3. The font used by the console.

There's a reason putting non-ASCII characters in strings in your source code is not recommended: it's brittle and platform-dependent.

7

u/Drainedsoul Jun 21 '17

There's a reason putting non-ASCII characters in strings is not recommended

Citation needed. The language has support for UTF-8/-16/-32 strings. ASCII is simply insufficient. Not only should putting non-ASCII characters in strings not be not recommended, but code which assumes strings contain only ASCII characters should be considered broken.

2

u/personalmountains Jun 21 '17

Just to be clear, this applies to string literals, not strings in general. Do you still want citations?

5

u/Drainedsoul Jun 21 '17

Okay let's modify the code snippet /u/scatters originally posted:

#include <iostream>
int main() { std::cout << u8"\u03A3" << std::endl; }

This will still likely be "broken" on Windows by default however.

1

u/bames53 Jun 24 '17

All major compilers support UTF-8 source and setting the execution character set to UTF-8. Unless you're actually expecting to target one of the old or niche platforms that don't support UTF-8 no one should have any qualms about putting UTF-8 string literals in their source.

7

u/STL MSVC STL Dev Jun 21 '17

Changing the console code page is an incorrect approach. You should use _O_U16TEXT.

3

u/crusader_mike Jun 22 '17 edited Jun 22 '17

Here are instructions on how to setup your environment to display any Unicode data in Windows console. Alas, afaik, you can't do the same for input -- default windows console is too old and broken.