This question already has an answer here:
-
Output Unicode to console Using C++, in Windows
4 answers
I haven't been able to find a way to cout
a '—' character, whether I put that in the cout statement like this: cout << "—";
or use char(151)
, the program prints out a fuzzy undefined character. Do you guys see anything wrong with my code? Is cout
ing a EM DASH even possible?
Edit: I've also tried wcout << L"—";
and std::wcout << wchar_t(0x2014);
. Those both print nothing in my terminal.
First of all, EM DASH is an unicode character (just making sure you do know that).
Printing unicode characters depends on what you're printing to.
If you're printing to a Unix terminal (or an emulator), the terminal emulator is using an encoding that supports this character, and that encoding matches the compiler's execution encoding, then you can do what you just did above in your source code cout << "—";
If you're getting fuzzy undefined characters, it is possible that your terminal just doesn't support that character.
If you're in windows (where it is harder), you can do something like this (which is not portable):
#include <iostream>
#include <io.h>
#include <fcntl.h>
int main() {
_setmode(_fileno(stdout), _O_U16TEXT);
std::wcout << L"—";
}
There's no universal support for Unicode in C++ and in various terminals, so there won't be a portable solution.
The thing is that Windows uses codepages in console by default. It probably uses UTF-16 internally but will always convert to and from ANSI codepage when interacting with outside. So printing an UTF-16 code point like std::wcout << wchar_t(0x2014);
won't work.
If you're using Windows in English or some other Western European languages that use codepage 1252 then you can print em-dash which is at the codepoint 151 simply by
cout << (char)151;
If it doesn't work then you're not on codepage 1252. You can change it to 1252 if possible or look up for em-dash in your codepage (if available)
Otherwise the simplest solution is switching to UTF-8 by chcp 65001
then print the character out by
wcout << L"—";
it will not always work because of the worse Unicode support in Windows console. In many cases the characters don't appear, replacing by squares or ????
but just copy the text out and paste to any Unicode text box then it will be displayed properly
On Linux things are much simpler because UTF-8 are used by default. So you can use
cout << "—";