Displaying Special Characters in C++

May 7, 2012 at 11:55am
Does anyone know how to output special characters in C++ (to the console)? For example, ♠ ♥ ♦ ♣ (the four card suit symbols) . I am hoping for a very simple way to do this. If it helps, my IDE is NetBeans 7.0.1/7.1 with Cygwin.

Thanks
May 7, 2012 at 12:05pm
Give this a go.

1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;

int main()
{
   const char heart[] = "\xe2\x99\xa5";
   std::cout << heart << '\n';
}

If it works, you'll need to look up the codes for other symbols you want (or experiment; start by changing that 5).


Last edited on May 7, 2012 at 12:06pm
May 7, 2012 at 12:19pm
No...it gave me the following output:

♥

...so I'm not really sure what's going on. Any help is still much appreciated though, so anyone who knows how to fix this, please help. Just FYI, my OS is Windows, which I think makes a difference.
May 7, 2012 at 1:04pm
try this:
1
2
3
4
5
6
7
8
9
#include <iostream>
using namespace std;

int main ()
{
    char heart = 3;             //get yourself an IBM ASCII chart for them all
    cout << heart << endl;//make sure it has the heart on it, because if it 
    system("pause");        //doesn't, it isn't accurate
}


I'd reccomend using google images to get a chart. MAKE SURE #3 is a heart, or it isn't accurate
May 7, 2012 at 1:09pm
what comes out depends on the code page that's currently active. In the linux world there's often UTF-8

http://en.wikipedia.org/wiki/Code_page


Edit: under windows it's 850:

http://en.wikipedia.org/wiki/Code_page_850
Last edited on May 7, 2012 at 1:11pm
May 7, 2012 at 2:54pm
I really appreciate the help and links, but I cannot seem to figure it out. I would really appreciate it if someone could give a specific example so I can see how to do this. Thanks!
May 7, 2012 at 3:58pm
const char* signs="\3\4\5\6";
That gives you, in order, a heart, a diamond, a spade and a club, if I remember their names correctly.
May 7, 2012 at 4:35pm
Why I try that, nothing is outputted (or maybe just whitespace). I'm really not sure what is going on.
May 7, 2012 at 4:48pm
Unfortunately the code page 850 doesn't contain the the symbols you want.

Are you able to access the win api?

SetConsoleOutputCP is the way to change the codepage. Look at this:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms686036%28v=vs.85%29.aspx

You need to find a code page that contains those symbols. UTF-8 would be the best if possible.
May 7, 2012 at 5:33pm
Unfortunately, WinAPI confuses me too much to effectively use it. I am still searching, but to no avail. Thanks for all the help though!
Last edited on May 7, 2012 at 5:34pm
May 7, 2012 at 5:47pm
Can't you guys just use std::wcout << L"\u2660\u2665\u2666\u2663"? All this codepage business is so 1990s.

(on windows, to configure the console for wide char output, do _setmode(_fileno(stdout), _O_WTEXT); )
Last edited on May 7, 2012 at 5:50pm
May 8, 2012 at 5:43am
You might want to take a look at ncurses:

http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/
May 8, 2012 at 5:50am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main()
{
	char a[4] = {3,4,5,6};

	cout<<"Heart: "<<a[0]<<endl;
	cout<<"Club: "<<a[1]<<endl;
	cout<<"Diamond: "<<a[2]<<endl;
	cout<<"Spade: "<<a[3]<<endl;

	system("PAUSE");
	return 0;
}
May 11, 2012 at 8:35pm
Sorry, but nothing has worked so far. Thanks for the help though :)
May 16, 2012 at 10:56pm
Well, I'm using the Allegro graphics library now, so that is probably just going to work better than anything else. If anybody knows how to do it through the console, please feel free to tell me. Thanks for all the help!
Topic archived. No new replies allowed.