9

So I'm currently learning C++11 and I'm getting the hang of it. I want to play around with using a different language and since I'm Vietnamese, I want to make a C++ program that uses Vietnamese characters.

So how can I display Vietnamese characters the same way that English is displayed, which is like this:

cout << "Hello. This is English" << endl; //English
cout << "Chào. Đây là tiếng Việt." << endl; //Vietnamese

I heard that C++ has <locale>. Does it help make the Vietnamese characters appear?

Starchipper
  • 167
  • 3
  • 11
  • Your question doesn't say what problem you're running into...? – jtbandes Jan 14 '17 at 03:32
  • You need to use `wcout` etc. Look up wide characters/strings – Ed Heal Jan 14 '17 at 03:39
  • `#include `, `std::setlocale(LC_ALL, "");`, `std::wcout << L"Chào. Đây là tiếng Việt."` .. works on all platforms .. but what system are you on? Windows might not display the characters without the language pack installed; this code works great on one of my Linux boxes with a bunch of locales installed, but my BSD displays only what it can because it does _not_ have the local installed .. – txtechhelp Jan 14 '17 at 03:45
  • @nikau6 .. how so? That exact code worked as expected on my Windows system .. – txtechhelp Jan 14 '17 at 03:53
  • @txtechhelp This character 'ế' cannot be displayed correctly. – nikau6 Jan 14 '17 at 04:28
  • @txtechhelp No, in fact the problem with the character 'ế' is with the 2 answers. With yours I get the output : "Chào. Đây là ti", nothing more. – nikau6 Jan 14 '17 at 04:39
  • 1
    Just overflow a buffer or two, perform some output and with any luck, Vietnamese glyphs will aappear. – Kaz Jan 14 '17 at 06:12
  • @jtbandes this isn't a debugging question. – Robert Columbia Jan 14 '17 at 13:30
  • It depends on your OS and compiler. – n. m. could be an AI Feb 08 '17 at 22:37
  • @nikau6 if you can see it in your browser, try to copy and paste it to your terminal. Can you do that? – n. m. could be an AI Feb 08 '17 at 22:39
  • 2
    Possible duplicate of [Output unicode strings in Windows console app](https://stackoverflow.com/questions/2492077/output-unicode-strings-in-windows-console-app) – phuclv Jul 28 '18 at 10:14

2 Answers2

8

You may be running into a problem with your environment. You don't say what platform/environment you are running in, but take the following program:

#include <iostream>
#include <cstdlib>

int main()
{
    std::cout << u8"Chào thế giới!" << std::endl;
    return EXIT_SUCCESS;
}

This yields the following output from iTerm on Mac OS X: Chào thế giới!

With other (non-unicode) environments, using the same code, you may get UTF-8 characters interpreted as ASCII on output. I don't know what the Windows command line will yield, but if you are using an IDE, your IDE may or may not render UTF-8, independently of whether your shell does or doesn't.

Here's a web example.

https://code.sololearn.com/c39N9RN6b4Md/#cpp yields: Chào thế giới!

But http://ideone.com/OkkUZs running exactly the same code yields: Chào thế giới!

It's probably also worth pointing out that in C++ to properly process UTF-8 strings, count "characters", ensure your strings are valid UTF-8, etc. you will likely want to use a Unicode library--working with Unicode is non-trivial.

Personally, I have found both UTFCPP and TinyUTF8 to be excellent libraries - reasonably small, simple and effective.

Hope that helps.

U007D
  • 5,772
  • 2
  • 38
  • 44
3
#include <iostream>
#include <io.h>
#include <fcntl.h>

int main() {
    _setmode(_fileno(stdout), _O_U16TEXT);
     std::wcout << L"Chào. Đây là tiếng Việt.";
}

This is a solution that works for windows. Unfortunately it's not portable to other platforms.

ScY
  • 91
  • 1
  • 7
  • This doesn't work on my system, but perhaps that's a police issue. I cannot display the character : `ế`, does it work for you? My solution has the same problem. – nikau6 Jan 14 '17 at 04:31
  • Yes it is displayed properly. What do you get instead? – ScY Jan 14 '17 at 04:55
  • The irony is that your unportable code is also not needed on most other platforms. Can't you set the output to UTF8 though? – dascandy Feb 09 '17 at 07:41