views:

311

answers:

4

I'm still learning C++, so bear with me and my sloppy code. The compiler I use is Dev C++. I want to be able to output Unicode characters to the Console using cout. Whenver i try things like:

# #include directive here (include iostream) 

using namespace std;

int main()
{

    cout << "Hello World!\n";
    cout << "Blah blah blah some gibberish unicode: ĐĄßĞĝ\n";
    system("PAUSE");
    return 0;
}

It outputs strange characters to the console, like µA■Gg. Why does it do that, and how can i get to to display ĐĄßĞĝ? Or is this not possible with Windows?

+1  A: 

What about std::wcout ?

#include <iostream>

int main() {
    std::wcout << L"Hello World!" << std::endl;
    return 0;
}

This is the standard wide-characters output stream.

Still, as Adrian pointed out, this doesn't address the fact cmd, by default, doesn't handle Unicode outputs. This can be addressed by manually configuring the console, like described in Adrian's answer:

  • Starting cmd with the /u argument;
  • Calling chcp 65001 to change the output format;
  • And setting a unicode font in the console (like Lucida Console Unicode).

You can also try to use _setmode(_fileno(stdout), _O_U16TEXT);, which require fcntl.h and io.h (as described in this answer, and documented in this blog post).

Tyn
This doesn't address the fact that the console is typically in ANSI or OEM mode.
Adrian McCarthy
+1  A: 

First of all you should use Unicode strings in your code instead of ASCII (L"text" instead of "text") and change/verify in the "Command Prompt" that you use Unicode font.

Oleg
+2  A: 

You used the ANSI output stream. You need to use
std::wcout << L"Blah blah blah some gibberish unicode: ĐĄßĞĝ\n";

Also, use std::cin.get(), not system("PAUSE")

DeadMG
Thanks for the tip about cin.get(). I know using system("PAUSE"); is a bad habit, but Dev C++ didn't support anything else I used. Also, wcout isn't recognized by Dev C++. I think I'll follow the advice in the other answers/comments and switch to Visual Studio. I experience fewer problems with that IDE.
Jesse Foley
+2  A: 

I'm not sure Windows XP will fully support what you need. There are three things you have to do to enable Unicode with a command console:

  1. Start the command window with cmd /u. The /u says your programs will output Unicode.
  2. Use chcp 65001 to indicate you want to use UTF-8 instead of one of the code pages.
  3. Select a font with more glyph coverage. The command windows in newer versions of Windows offer Lucida Console Unicode. My XP box has a subset of that called Lucida Console. It doesn't have a very extensive repertoire, but it should be sufficient if you're just trying to display some accented characters.
Adrian McCarthy