Using Unicode

I'm trying to read, store, and output Unicode characters. Specifically █, ▓, ▒, and ░. I've googled some but haven't gotten anything to work. I am programming in visual studio 2010 (which is also brand new to me, used to using Linux), Windows 7.

Here is a code snippet:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

int main()
{
	wstring goo = L"█ ▓ ▒ ░";
	wcout << L"█ ▓ ▒ ░" << endl;
	wcout << goo << endl;
	int x;
	while(1)
	{
		cin >> x;
		if(x == 0)
		{
			return 0;
		}
	}
}


Lines 10 and 11 do nothing. It doesn't cout anything.

Please help. Thanks
Those characters may be non-printable.
Also, wcout probably isn't working the way you would like. I'm pretty sure it simply "narrows" the characters before printing them.
How can they be not printable?

What do you mean wcout narrows the characters? What can I do to make those characters print?
Last edited on
Basically, it takes the wide character and tries to convert it to a normal char. If the wide characters you are printing don't have a char (normal/narrow) version, it is filled with a default value. This could be a space, a null character, or something like that. It's possible they are just being narrowed to some non-printing character.

The thing is, C/C++ don't really have any support for Unicode. The easiest way I've found is to simply cout the characters in a normal string and have the console set up with to display the output properly. I recall there being quite a detailed topic about Unicode in C++, but I don't remember the title.
Also, wchar_t isn't really suitable for dealing with Unicode. If you really want to use Unicode you would either have to create an own Unicode character type for it (and write an output for them too), or you'd have to go find some external library that enables you to use Unicode characters. If you want to depend on getting the default literals to do what you want them to, you probably won't get very far with it.
@hanst99: You seem to be spreading a lot of false information about Unicode and trying to dissuade people from pursuing it.

Why all the hate? Unicode support should be present in all programs unless there's compelling reason to omit it.

Although in this case you do somewhat have a point. Unicode output with the windows console is difficult. wcout is pretty much useless.


@OP: If you're really interested, you can check this thread. Near the end I was able to get Unicode console output working, but you had to use Windows specific functions to do it (can't use cout):

http://www.cplusplus.com/forum/windows/9797/
Topic archived. No new replies allowed.