Outputting Special Characters in the console

Pages: 12
Aug 9, 2011 at 2:39pm
I was making a simple console program that generates two random 2-D Vectors, Adds them, subtracts them and gives the output in the the Unit-Vector firm as well as Magnitude form with Angle from the Co-ordinate Axes.

Now what I was interested to know was this:
Is there a way to output a special character (That is not in the ASCII list)
like î?

I am using Windows 7 if it matters.

EDIT:
I noticed that the character I used was not visible so clearly.
The character is i cap (like in 5î).
Last edited on Aug 9, 2011 at 2:41pm
Aug 9, 2011 at 3:15pm
closed account (zwA4jE8b)
from what I have read, you cannot display special characters in the console, but it does understand them.

you might just have to stick with i and j or use a combination, even though it doesn't look as nice ... "i^" or "j^"
Last edited on Aug 9, 2011 at 3:16pm
Aug 9, 2011 at 3:17pm
Well, thanks for the reply...
When I tried using that character, it gave me an Epsilon (The Greek Alphabet) on the console!
Aug 9, 2011 at 4:15pm
Usually when I output things to the console that fail the isprint(thechar) I end up outputting something like this cout << '[' << (int)((unsigned char)thechar) << ']';
Aug 9, 2011 at 7:08pm
The Windows console has a "code page" associated with it, which defaults to the old "Extended ASCII" set you see from DOS days. You can change the code page and then print stuff like that.

The Windows console can also display Unicode stuff if you output it in UTF-16LE. (Use a BOM.)

Good luck!
Aug 10, 2011 at 7:49am
@ulfinitus:
That didn't work either :(
BTW what is isprint(thechar)?

@Douas:
How can I edit the code page?(Where is it located? I can't seem to find it)
And how do we output something in UTF-16LE?
Last edited on Aug 10, 2011 at 7:55am
Aug 10, 2011 at 4:48pm
To write UNICODE chars (assuming LE is the standard Windows variant of UTF-16...) out with the iostream library, call _setmode() with _O_U16TEXT and then use wcout.

Note that cout doesn't work when this mode is set (well, I hit asserts...)

Also note that the console CP and the CRT CP are handled separately.

Andy

P.S. Duoas - can you use a BOM with a wostream?
Aug 10, 2011 at 4:57pm
P.P.S. If you want to get European accented ANSI chars to behave on an English version Windows, when using a console app, the following trick might be of use. I use it for little test tools for a bilingual program -- French/English -- when testing on an English system. It uses CharToOem() to work its magic.

If you use std::cin to read input you require the reverse mapping.

Also note that when this mechanism is operational, the strings written to file are confused. So you need it on to write to the console, but off to write to file. A bit of a pain...

Andy

"Accents dans une console windows"
http://www.developpez.net/forums/d19262/general-developpement/programmation-systeme/windows/accents-console-windows/
Last edited on Aug 10, 2011 at 4:59pm
Aug 10, 2011 at 5:11pm
Sorry to be non-responsive...
P.S. Duoas - can you use a BOM with a wostream?

Don't waste your time with wostreams... they don't do Unicode. (Which is incredibe, but true... See narrow() for all the disappointing details.)
Aug 10, 2011 at 5:39pm
I've successfully used wcout to write UNICODE strings to the console for wstrings containing standard European chars, and box drawing chars, to the console using _setmode. In this case I would expect narrow() to do nothing.

When you say "see narrow()... do you mean see the documentation? Or see the source code?)
Aug 10, 2011 at 6:48pm
Er, what do you mean? How does _setmode() affect it? Are you saying that the console stream needs to be in binary mode to work?

Also, did you actually output non-C0/C1 characters to the console, or did you output something with a code wider than eight bits?

The STL wide streams simply chop the input wchar_t to an eight-bit char before outputting to a char stream. (I think you have to install a special locale to prevent that, or somesuch, but that is, IIRC, more difficult than it should be, especially when working with multiple compilers/STL libraries/versions.)
Aug 10, 2011 at 7:13pm
Well, it's no doubt Microsoft CRT specific, but the original poster did mention Windows...

It's the _O_U16TEXT mode which makes the wchar_t display correctly.

I have displayed chars from the Box Drawing set ('\x2500' - ...) as well as regular C0/C1 chars.

See thread:
Unicode Box Drawing chars
http://www.cplusplus.com/forum/windows/48175/

Andy

P.S. I am genuinely interested in this issue. I am working on a mini project that currently uses Latin-1; we're avoiding Windows-1252 so we are totally safe on Windows, Linux, and Mac OS-X (are we being over neurotic?). But we are considering moving to UTF-8, which only Windows is awkward about (at least in earlier version). Or possibly UTF-16.
Last edited on Aug 10, 2011 at 7:14pm
Aug 10, 2011 at 9:03pm
#include<iostream>

using namespace std;

int main()
{
char x=140;

cout << x << endl;
return 0;
}
Aug 10, 2011 at 11:05pm
Hmm, I didn't know there was such a flag as _O_U16TEXT... LOL.
Aug 12, 2011 at 9:42am
Sorry, couldn't come here for a while. Was bust with my weekly exams.

I am having a problem though.
I am unable to use wcout.
I am using VS 2010 Professional, and it is giving me the Unidentified Identifier error
Last edited on Aug 12, 2011 at 9:46am
Aug 12, 2011 at 8:10pm
Should be there!

Help for vcout (vc10) says:

Header: <iostream>
Namespace: std

http://msdn.microsoft.com/en-us/library/sda3xxca%28v=VS.100%29.aspx

Andy
Aug 15, 2011 at 3:17pm
Again sorry for not replying. I had been out out of station for the last couple of days...

Well to the wcout now.
I had included <iostream> in another header that was included in the main program. Can't imagine why it wouldn't work, but Re-Including it did the work.

Now another problem.
If I am declaring a variable like this:
wchar_t icap = 'î';
and then using wcout, I print icap.
It prints ?.
Am I doing something wrong in declaring it?
Aug 15, 2011 at 3:29pm
Do you mean

wchar_t icap = L'î';

(whar_t literals need an L -- for long, I presume)

And have you called _setmode as described earlier in this thread?

Andy
Last edited on Aug 15, 2011 at 3:29pm
Aug 15, 2011 at 3:38pm
How are you saving your file?
(Choose something like UTF-8.)
If necessary, adjust your compiler flags to accept the input file type.
Aug 15, 2011 at 5:19pm
I believe the default character encoding used for source files by VC10 is UTF-8 ??
Last edited on Aug 15, 2011 at 5:22pm
Pages: 12